Assume that all variables without declaration shown in the following program segments have already been declared properly. Integers (integer/int) and long integers (longint/long) in problem statements are 16-bit signed variables and 32-bit signed variables respectively.
Notations
For any real number x,
(1) x denotes the largest integer not greater than x.
(2) x denotes the smallest integer not less than x.
Section A
For each question, choose the most appropriate answer and write the letter (A, B, C, D) in the corresponding space on the answer sheet.
1. In chess, a knight is a piece that has the following moves:
As illustrated above, a knight's move consists of stepping two squares horizontally or vertically, then one square in a perpendicular (90 degree) direction like an "L" shape. Note that the knight can move to the destination square even if there are obstacles in its path.
Let us assume that knights "attack" any other chess piece if that chess piece is on a square where the knight can move to. At most how many knights can be placed on a 4x4 chessboard without attacking each other?
A. 7 B. 8 C. 9 D. 10
2. What is the output of the following program?
Pascal Version
program SectA_Q2;
var n: integer;
begin
n := 13;
while (n<>0) do begin
write(n mod 2);
n := n div 2;
C Version
#include <stdio.h>
int main() { int n;
n = 13;
while (n!=0) {
printf("%d", n%2);
n /= 2;
}
A. 1101 B. 1011 C. 0011 D. 0110
3. Chris has an unlimited number of $2, $7 and $10 coins. He wants to pay an amount of $48 exactly. What is the minimum number of coins required?
A. 6 B. 7 C. 8 D. 9
4. Let F be a sequence defined by F0 = 0, F1 = 1, F2 = 4, and Fn = Fn-1 + Fn-2 + Fn-3 for n > 2. Which of the following is/are true?
(i) F is non-decreasing.
(ii) Fn is greater than 3Fn-1 for all n > 0.
(iii) Fn is odd if and only if n is odd.
A. (i) only B. (iii) only C. (i) and (iii) only D. (i), (ii) and (iii)
5. Let A[1..n] (A[n] in C version) be an array of integers. swap(x, y) is a function that exchanges the values stored in variables A[x] and A[y]. Consider the following program segment:
Pascal Version
for i := 1 to n do
for j := i downto 2 do swap(j, j-1);
C Version
for (i=0; i<n; i++) for (j=i; j>0; j--)
swap(j, j-1);
If n = 5 and A contains (2, 5, 4, 3, 1) initially, what will be the content of A after the above program segment has been executed?
A. (1, 2, 3, 4, 5) B. (5, 4, 3, 2, 1) C. (2, 5, 4, 3, 1) D. (1, 3, 4, 5, 2)
6. Which of the following algorithms CANNOT be performed on a sorted linked list?
(i) Binary search (ii) Linear search (iii) Bubble sort
Who is the tallest?
A. P B. Q C. R D. T
8. Suppose x and y are integers and y is non-zero. Which of the following statements MAY change the value of x?
Pascal Version
(i) x := x + y – y;
(ii) x := x * y div y;
C Version
(i) x = x + y - y;
(ii) x = x * y / y;
A. (i) only B. (ii) only C. (i) and (ii) D. None of them
9. What is the output of the following program?
Pascal Version
program SectA_Q9;
var a, b, c, d, i : integer;
begin a := 1;
b := 1;
c := 1;
for i := 1 to 5 do begin
d := a + b + c;
a := b;
b := c;
c := d;
end;
writeln(b);
end.
C Version
#include <stdio.h>
int main() {
int a, b, c, d, i;
a = 1;
b = 1;
c = 1;
for (i=1; i<=5; i++) { d = a + b + c;
a = b;
b = c;
c = d;
}
printf("%d\n", b);
} A. 9
B. 17 C. 31 D. 57
10. What is the output of the following program segment?
Pascal Version
writeln(-9 mod 5);
C Version
printf("%d\n", -9 % 5);
A. 4 B. -4 C. 1 D. -1
Read the following paragraph, then answer questions 11-13.
Let A be an array of integers. The first n integers in A are sorted in ascending order. You are given a function swap(x, y) which exchanges the values of A[x] and A[y].
11. If you want to rearrange the first n+1 integers in ascending order, at most how many times do you need to call swap(x, y)?
A. n-1 B. n C. n+1 D. n+2
12. If you want to rearrange the first n integers in descending order, at most how many times do you need to call swap(x, y)?
A. n B. n-1 C. n/2 D. n/2
13. If you want to rearrange the first n+1 integers in descending order, at most how many times do you need to call swap(x, y)?
A. n-1 B. n C. n+1 D. nn/2
14. Which of the following expressions evaluate(s) to true if and only if the 16-bit signed integer k is odd?
Pascal Version
(i) k mod 2 = 0 (ii) k and 1 = 1
(iii) k shl 15 = -32768
C Version
(i) k % 2 == 0 (ii) (k & 1) == 1
(iii) (k << 15) == -32768 A. (i) only
B. (ii) only C. (i) and (ii) only D. (ii) and (iii) only
15. Which of the following is NOT a stack operation?
A. Determine whether a stack is empty.
B. Initialize an empty stack.
C. Remove the top element of a stack.
D. Insert an element at the bottom of a stack.
16. A positive integer n is a sum of squares if n = x12+x22+…+xk2, where x1, x2, …, xk are distinct positive integers less than 10. For example, 41 = 12 + 22 + 62 is a sum of squares, but 2 is not. Chris uses the following function to check whether n is a sum of squares.
Pascal Version
function f(n: integer) : boolean;
var i: integer;
begin
for i := 9 downto 1 do if (n>=i*i) then
n := n–i*i;
f := (n = 0);
end;
C Version
#define true 1
#define false 0 int f(int n) {
int i;
for (i=9; i>0; i--) if (n>=i*i)
n -= i*i;
return (n == 0);
}
However, f does not always give the correct answer. What is the problem with f?
A. It always returns true.
B. It always returns false.
C. It may return false when n is a sum of squares.
D. It may return true when n is not a sum of squares.
17. Consider the following program segment:
Pascal Version
function f(n: integer) : integer;
begin
if n > 1 then
f := f(n div 2) + f(n div 2) else
f := 1;
end;
C Version
int f(int n) { if (n > 1)
return f(n/2) + f(n/2);
return 1;
}
If n > 0, what is the value returned by f(n)?
A. log2n
B. n C. 2n
D. The largest power of 2 not greater than n, i.e. 2log2n. 18. How many ways are there to go from A to B?
A. 10 B. 11 C. 12 D. 13
19. What is the purpose of the following program segment?
Pascal Version
function f(a, b: integer) : integer;
var result, power: integer;
begin
result := 0;
power := 1;
while (a>0) or (b>0) do begin
if a mod 2 + b mod 2 > 0 then result := result + power;
power := power * 2;
a := a div 2;
b := b div 2;
end;
f := result;
end;
C Version
int f(int a, int b) { int result, power;
result = 0;
power = 1;
while (a>0 || b>0) { if (a%2+b%2 > 0)
result += power;
power *= 2;
a /= 2;
b /= 2;
}
return result;
}
A. It outputs the bitwise OR of a and b.
B. It outputs the bitwise AND of a and b.
C. It outputs the sum of a and b.
D. It outputs the product of a and b.
Suppose function max(a, b)and function min(a, b) return the maximum and minimum of two integers a and b respectively. If a is not equal to b, which of the following program segments guarantees that a > b after execution?
Pascal Version A.
a := max(a, b);
b := min(a, b);
B.
a := a + b;
b := b – a;
C.
a := max(a, -b);
b := max(-a, b);
D.
a := max(a, -a);
b := min(b, -b);
C Version A.
a = max(a, b);
b = min(a, b);
B.
a = a + b;
b = b – a;
C.
a = max(a, -b);
b = max(-a, b);
D.
a = max(a, -a);
b = min(b, -b);
20. Given two strings x and y of the same length, the Hamming distance between them, denoted by d(x, y), is the number of differing corresponding characters, i.e. the number of positions k such that x[k]≠y[k]. For example, the Hamming distance between “hkoi” and “hong” is 3; the Hamming distance between “tones” and “roses” is 2. Given any three strings x, y and z of the same length, which of the following MUST be true?
(i) If d(x, y) = 0, then x and y are equal.
(ii) d(x, y) = d(y, x)
(iii) d(x, y) d(x, z) + d(z, y) A. (i) and (ii) only
B. (i) and (iii) only C. (ii) and (iii) only D. (i), (ii) and (iii)
21. Consider the following program segment:
Pascal Version
procedure f(a, b: integer);
begin
write('*');
if b-a > 1 then begin
f(a, (a+b) div 2);
f((a+b) div 2 + 1, b);
end;
end;
C Version
void f(int a, int b) { printf("*");
if (b-a > 1) { f(a, (a+b)/2);
f((a+b)/2+1, b);
} }
It is given that when f(1, 3000) is called, 3951 ‘*’’s are outputted. If f(1,12000) is called, how many ‘*’’s will be outputted?
A. 15804 B. 15807 C. 15809 D. 63231
Read the following program segment, then answer questions 23-24.
Pascal Version
function f(n: integer) : integer;
begin
if n=1 then f := 1
else if (n mod 2) = 0 then f := f(n div 2) + 1 else
f := f(3*n+1) + 1;
end;
C Version
int f(int n) { if (n==1)
return 1;
else if (n%2==0) return f(n/2) + 1;
else
return f(3*n+1) + 1;
}
22. What is the value returned by f(10)?
A. 5 B. 6 C. 7 D. 8
23. Which of the following statements is NOT correct?
A. f(8) < f(10)
B. f(2k) = k + 1 for any positive integer k <= 14 C. f(2006) = f(2674)
D. f(2006) = f(12040)
24. The following diagram shows the map of a museum, where a circle represents a room and a straight line represents a passage between two rooms. Two rooms are adjacent if they are directly connected by a passage.
We are going to install cameras in some of the rooms. The range covered by a camera includes the room in which it is installed and all rooms adjacent to it. For example, if we install a camera in room G, the camera will cover the rooms G, E, F, H, K and L. What is the minimum number of cameras required to cover all rooms?
A. 3 B. 4 C. 5 D. 6
25. Let A and B be two sequences of real numbers such that Bn = (A1 + A2 + … + An) / n for n 1. If B is non- decreasing, which of the following statements MUST be true?
26. Consider a type of relationship “friendship”, which has the following two properties:
-- If x is a friend of y, then y is a friend of x.
-- If x is a friend of y and y is a friend of z, then x is a friend of z.
There are seven students P, Q, R, S, T, U and V. We know that the following statements are true:
(a) P is a friend of Q.
(b) R is a friend of Q.
(c) S is a friend of T.
(d) U is a friend of V.
Which of the following statements MUST be true?
(i) R is a friend of P.
(ii) T is a friend of Q.
A. (i) only B. (ii) only C. (i) and (ii) D. None of them
27. This question follows question 27. Besides “friendship”, there is another type of relationship “enemy” which has the following three properties:
-- If x is an enemy of y, then y is an enemy of x.
-- If x is an enemy of y and y is a friend of z, then x is an enemy of z.
-- If x is an enemy of y and y is an enemy of z, then x is a friend of z.
Now, in addition to (a), (b), (c) and (d) given in the previous question, the following statements are true:
(e) P is an enemy of U.
(f) S is an enemy of V.
Which of the following statements MUST be true?
(i) R is an enemy of S.
(ii) Q is a friend of T.
A. (i) only B. (ii) only C. (i) and (ii) D. None of them
28. Let A[1..10] (A[10] in C version) be an array of integers and rand(n) be a function which returns a positive random integer less than n. Consider the following program segment:
Pascal Version n := 10000;
A[1] := 0;
for i := 2 to 9 do A[i] := rand(n);
C Version n = 10000;
A[0] = 0;
for (i=1; i<9; i++) A[i] = rand(n);
Assume that the above program segment takes exactly one second to run on a certain machine during an execution.
If the first line is changed to n := 30000; (n = 30000; in C version), which of the following is true?
A. The new program segment takes approximately 1 second to run on the same machine.
B. The new program segment takes approximately 3 seconds to run on the same machine.
C. The new program segment takes approximately 9 seconds to run on the same machine.
D. Since the content of A is unknown, the execution time of the new program segment can vary greatly.
29. Simplify the following Boolean expression:
((P and Q) or (P and not Q)) and (not P or R) A. P and R
B. (P and Q) or R C. true
D. None of the above
END OF SECTION A
Section B
The blanks are labeled from A to J. Please fill in the blanks on the answer sheet.
Note:
(1) You must not use the ?: operator in C.
(2) You must not use any library function unless the appropriate library(s) is/are included.
1. Given integers n and m (-1000 < n < 1000 and 0 < m < 1000), the following program computes the smallest non- negative integer x such that n-x is divisible by m. For example, if n = 12, m = 5, then x = 2. Fill in the blank.
Pascal Version
program SectB_Q1;
var n, m: integer;
begin
readln(n, m);
writeln('x = ', A );
end.
C Version
#include <stdio.h>
int main() { int n, m;
scanf("%d %d", &n, &m);
printf("x = %d\n", A );
return 0;
}
2. You are given a function max3(a, b, c) which returns the maximum of three integers a, b and c (0 < a, b, c
< 10000). The following function returns the minimum of a, b and c. Fill in the blank using max3.
Pascal Version
function min3(a, b, c: integer) : integer;
begin
min3 := B ; end;
C Version
int min3(int a, int b, int c) { return B ;
}
The following function returns the median of a, b and c. Fill in the blank using max3 and min3.
Pascal Version
function median3(a, b, c: integer) : integer;
begin
median3 := C ; end;
C Version
int median3(int a, int b, int c) { return C ;
}
The following program reverses the order of elements in the array A. Fill in the blank.
Pascal Version
program SectB_Q3;
var
A: array[1..10] of integer;
t, i: integer;
begin
for i := 1 to 10 do begin
t := A[ D ];
A[ D ] := A[i];
A[i] := t;
end;
end.
C Version
#include <stdio.h>
#include <stdlib.h>
int main() {
int A[10], t, i;
for (i=0; i<10; i++) { t = A[ D ];
A[ D ] = A[i];
A[i] = t;
}
return 0;
}
3. *-*-*-*-*-
-*---*---*
*-*-*-*-*- -*---*---*
*-*-*-*-*- -*---*---*
*-*-*-*-*- -*---*---*
*-*-*-*-*- -*---*---*
The following program segment outputs the above pattern. Fill in the blank.
Pascal Version
for i := 1 to 10 do begin
for j := 1 to 10 do if E then
write('*') else
write('-');
writeln;
end;
C Version
for (i=1; i<=10; i++) { for (j=1; j<=10; j++)
if E printf("*");
else
printf("-");
printf("\n");
}
4. Consider the following program segment:
Pascal Version
function f(s: string) : string;
var
i, n, x: integer;
begin
n := length(s);
for i := 1 to n do begin
x := ord(s[i]) – ord('a');
x := x + 13;
x := x mod 26 + ord('a');
s[i] := chr(x);
end;
f := s;
end;
C Version
char *f(char *s) { int i, n, x;
char *s2;
n = strlen(s);
/* allocate n bytes of memory for s2 */
s2 = (char *) malloc(n);
for (i = 0; i < n; ++i ) { x = s[i] - 'a';
s2[i] = ((x + 13) % 26) + 'a';
}
return s2;
}
(a) What is the string returned by f('envy') ( f("envy") in C version)? F
(b) What is the string returned by f(f('computerprogrammingcomputerprogramming')) (f(f("computerprogrammingcomputerprogramming")) in C version)? G
5.
Given 6 numbers b1, b2, b3, b4, b5 and b6, where 0 < b1 < b2 < b3 < b4 < b5 < b6, Chris wants to find all possible ways to fill in the above boxes such that:
(i) Every number is used exactly once.
(ii) If a number is not in the bottom row, then it is equal to the absolute difference between the two numbers below it. (i.e. a1 = |a2 - a3|, a2 = |a4 - a5| and a3 = |a5 - a6|.)
Chris uses the following algorithm to solve this problem:
Try all possible permutations of b1, b2, b3, b4, b5, b6 to fill in the boxes. For each permutation, check whether the permutation satisfies (ii).