Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse...
-
Upload
evan-barnett -
Category
Documents
-
view
257 -
download
0
Transcript of Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse...
Introduction to Computers
-Final exam-授課教授:李錫智
Q1
• Write a function named Reverse that takes a string as input and returns a copy of that string in reverse order. For example, the function call Reverse("apple") should return the string "elppa".
Solution of Q1
• Ans:
function Reverse(str)
{
var len,restr;
len=str.length;
restr="";
i=len-1;
while(i!=-1)
{
restr=restr+str.charAt(i);
i=i-1;
}
}
Q2
• Consider the following program :function string(string){
str = string;i = str.search(/[aeiou]/);while(i != -1){
str = str.substring(0,i) + '*' + str.substring(i+1,str.length);
i = str.search(/[aeiou]/);}return str;
}
• What will be returned for the function call string('Hello world')?
Solution of Q2
• Ans: H*ll* w*rld
Q3
• Write a function named Maximum that takes an array of numbers as input and return the biggest value in the array. For example, the call Maximum([2, 28, 33, 12, 5, 17, 8]) should return 33.
Solution of Q3
Q4
• Suppose you are given the following segment of program:
Phrase = “George Bush, USA\n”+“Leonardo da Vinci, Italy\n”+“William Shakespeare, England\n”;Spilt_1 = Phrase.split(“, “);Spilt_2 = Phrase.split(/[,\n]+/);
• [5] What is the value of Split_1?
• [5] What is the value of Split_2?
Solution of Q4
• Ans:• Split_1 = ["George Bush", "USA\nLeonardo da Vinci",
"Italy\nWilliam Shakespeare", "England\n"] ;• Split_2 = ["George Bush", "USA", "Leonardo da Vinci",
"Italy", "William Shakespeare", "England"];
Q5
• Please using the NOT, AND, and OR gates, draw a circuit that will output 0 while both inputs are 0 or 1. That is, suppose A and B are the two inputs and C is the output, then C=0 if and only if (A=0 and B=0) or (A=1 and B=1).
Solution of Q4
A B C
0 0 0
0 1 1
1 0 1
1 1 0
Q6• Consider the following program:
• What will be returned for the function call Cipher('Bronze')?
function Cipher(str){
var coded, ch, index, key, i;alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';coded = ' ';key = 4;str = str.toUpperCase();i = 0;while(i < str.length){
ch = str.charAt(i);index = alphabet.search(ch);if (index != -1) {
if ( (index+key) < alphabet.length) {coded = coded + alphabet.charAt(index+key);
}else {coded = coded + alphabet.charAt(index+key-alphabet.length);
}}else {
coded = coded + ch;}i = i + 1;
}return coded;
}
Solution of Q6
• Ans: FVSRDI
Offset 4 characters.
Q7
• Suppose you are given an array of numbers, A.
• (a) [10] Please write a function taking A and returning an array B which records the number of times each element appearing in A. For example, suppose A=[15,25,35,45,35,35,25]. Then B=[1,2,3,1,3,3,2].
• (b) [10] Please write a function taking A and returning the element in A which appears most frequently in A. For example, for the above example, 35 will be returned.
Solution of Q7
Q8
• Please design a Web page that asks the user to enter his/her name and age in a phrase separated by a colon, e.g., John :36.
• When the user clicks a button, the webpage will show the name and say the user is an adult or a child, e.g., John is an adult.
• Note that a person is an adult if his/her age is equal to or greater than 20.
Solution of Q8
Q9
• Consider the following circuit. A, B, and C are inputs. P and Q are outputs. Please list the input-output relationship in a truth table.
Solution of Q9
A B C Q P
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
Q10
• Please write a function to check whether a given string is a mirror-string. A string A is a mirror-string if and only if A satisfies one of the following conditions:• A is a concatenation of B and C, i.e., A=BC, where B
and C are strings and C is the reverse of B. For example, “abccba” is a mirror-string.• A is a concatenation of B, t, and C, i.e., A=BtC, where B
and C are strings, t is a character, and C is the reverse of B. For example, “abcycba” is a mirror-string.
Solution of Q10
• Ans :
function IsP(string) {
var stripped, front, back;
stripped = string;
front = 0;
back = stripped.length - 1;
while (front < back) {
if (stripped.charAt(front) != stripped.charAt(back)) {
return false;
}
front = front + 1;
back = back - 1;
}
return true;
}
The end of the final Exam