All program segments in the following questions are assumed to be compiled with Turbo Pascal 7.0 compiler. Assume that all variables without declaration shown have already been declared properly.
Please refer to the following description for questions 1 to 3:
The fibonacci sequence {an} is defined as an = an-1 + an-2 for n = 2, 3, 4, …, and a0 = a1 = 1. A teacher asked 3 of his students to write a program to find a10. Here are their programs:
Student 1 program fibonacci_A;
const n=10;
function a(n: integer): longint;
begin
if n<2 then a:=1 else
a:=a(n-1)+a(n-2);
end;
begin
writeln(a(n));
end.
Student 2 program fibonacci_B;
const n=10;
var a: array[0..n] of integer;
i: integer;
begin a[0]:=1;
a[1]:=1;
for i:=2 to n do
a[i]:=a[i-1]+a[i-2];
writeln(a[n]);
end.
Student 3 program fibonacci_C;
const n=10;
var a,b,c,i: longint;
begin a:=1;
b:=1;
for i:=2 to n do begin
c:=a+b;
a:=b;
b:=c;
end;
writeln(c);
end.
1. Which of his student(s) get the correct answer?
A. Student 2 only B. Student 1 and 2 only C. Student 2 and 3 only D. All of them
2. Now the teacher wants to find a1 with these 3 programs. He simply changes the line "const n=10;" to "const n=1;", then re-compiles and runs the programs. Which of the above programs give(s) the correct answer?
A. Student 1 only B. Student 1 and 2 only C. Student 2 and 3 only D. All of them
3. Furthermore, the teacher wants to check the programs with a40. He knows that the answer is 165580141. He changes the line "const n=10;" to
"const n=40;", then re-compiles and runs the programs. Which of the above programs give(s) the correct answer?
A. Student 1 only B. Student 2 only C. Student 3 only D. Student 1 and 3 only
4. What is the output of the following program segment?
var
s,t: string;
i: integer;
begin
s:='HKOI2002';
t:='';
for i:=length(s) downto 3 do t:=s[i]+t;
writeln(t);
end.
A. HKOI2002 B. OI2002 C. 2002IO D. 02IOKH
Please read the following description to answer questions 5 to 6.
This program is to put six integers 1 to 6 into the six boxes in the triangle below.
Each integer can be used once only. The sums of the integers in the three boxes on each side of the triangle should be the same.
var
a:array[1..6] of integer;
b:array[1..3] of integer;
i,j,s:integer; ok:boolean;
begin
randomize;
for s:=1 to 1000 do begin
for i:=1 to 6 do begin ok:=true;
repeat
a[i]:=random(6)+1;
for j:=1 to i-1 do if missing 1 then ok:=false;
until ok;
end;
b[1]:=a[6]+a[1]+a[2];
b[2]:=a[2]+a[3]+a[4];
b[3]:=a[4]+a[5]+a[6];
if missing 2 then begin
for i:=1 to 6 do write(a[i],' ');
writeln;
end;
end;
end.
5. What should be missing 1 ? A. a[i]=a[j]
B. a[i]<>a[j]
C. a[j]=a[j-1]
D. a[j]<>a[j-1]
6. What should be missing 2 ?
A. a[1]+a[2]+a[3]+a[4]+a[5]+a[6] = b[1]+b[2]+b[3]
B. a[1]+a[2]+a[3]+a[4]+a[5]+a[6] <> b[1]+b[2]+b[3]
C. (b[1] = b[2]) and (b[2] = b[3]) and (b[3] = b[1]) D. (b[1] <> b[2]) or (b[2] <> b[3]) or (b[3] <> b[1])
7. Which of the following variable type cannot be used as cases in a ‘case’
statement?
A. integer B. char C. string D. boolean
8. How many distinct 3-digit integers can be generated by the following digits?
0, 2, 2, 3, 6 A. 26
B. 33 C. 40 D. 48
9. What should the missing statement be such that the values of a and b are swapped after the execution of the program segment?
inc(a,b);
missing_
dec(a,b);
A. b:=a;
B. b:=a-b;
C. inc(b,a);
D. dec(b,a);
Please refer to this description to answer questions 10 to 11.
The following is a graph. Each dot in the graph is called a node, and the line is called an edge.
10. A bipartite graph is a graph whose nodes can be assigned a colour either red or blue, such that no edge joins two nodes of the same colour. What is the minimum number of edges to be removed from the above graph so that it becomes a bipartite graph?
A. 1 B. 2 C. 3 D. 4
11. What is the minimum number of edges to be removed so that the above graph becomes a Euler Graph? (That is to travel all the edges without repeating).
A. 0 B. 1 C. 2 D. 3
12. Which of the following statement is invalid?
A. ww : array [65535..65536] of longint;
B. xx : array ['a'..'c'] of char;
C. yy : array [-200..200] of integer;
D. zz : array [false..true] of boolean;
13. Which of the following program segment(s) will assign variable hkoistr as 'HKOI2002'?
(i) hkoistr:='HKOI+2002';
(ii) hkoistr:='HKOI20021'-'1';
(iii) hkoistr:=copy('I participate in HKOI2002 and I feel happy!!',18,8);
A. (iii) only B. (i) and (ii) only C. (i) and (iii) only D. (i), (ii) and (iii)
14. What is the output of the following program?
program add_subtract;
var
x,y,m,n:integer;
procedure add(var m,n:integer);
begin m:=m+n;
end;
procedure subtract(m,n:integer);
begin m:=m-n;
end;
begin
m:=3; n:=2; x:=5; y:=6;
add(x,y);
add(m,n);
subtract(x,m);
add(x,n);
writeln(x);
end.
A. 13 B. 8 C. 7 D. 5
15. According to the flow chart below, which of the followings is incorrect?
Input Output
A. 1 2 3 4 2
B. 2 4 6 8 2
C. 0 1 8 4 2
D. 2 0 1 2 2
Output A/B Output B/A
B<>0 A<>0
Y
N N
N Y Y
Start
End Input A,B
Output C/D Input C,D
A>=B
16. What is the output of the program segment?
var i,n: integer;
begin n:=0;
for i:=1 to 100 do if not i>40 then n:=n+1;
writeln(n);
end.
A. 0 B. 39 C. 40
D. none of the above
17. Let P, Q be boolean variables, consider the following boolean expressions:
(i) P or Q
(ii) not (P or Q)
(iii) (not P) and (not Q) (iv) (not P) or (not Q)
Which two of the above are always equivalent?
A. (i) and (iii) B. (i) and (iv) C. (ii) and (iii) D. (ii) and (iv)
18. What is the output of the program segment?
if 2>=2 then
if 'HKOI'='2002' then writeln('A')
else
if true>false then writeln('B')
else
writeln('C');
A. A B. B C. C D. no output
A. integer B. real C. longint D. pointer Questions 20–22:
Here is a program to print out all prime numbers from 1 to 10000.
program prime;
var i,j,k: integer;
isprime: boolean;
begin
for i:=2 to 10000 do begin
isprime:=true;
for j:=missing 1 to missing 2 do if (i mod j)=0 then
isprime:=false;
if missing 3 then writeln(i);
end;
end;
20. What should be filled in missing 1 ? A. 0
B. 1 C. 2 D. 3
21. What should be filled in missing 2 so that the execution time is the shortest?
A. 997 B. i-1
C. trunc(sqrt(i)) D. round(sqrt(i))
22. What should be filled in missing 3?
A. i<>j
B. (i mod j)<>0 C. j=i-1
D. isprime
23. Which of the following programming error(s) can be discovered at the time of compilation?
A. syntax error B. logical error
C. both of syntax and logical error D. neither syntax nor logical error 24. What is the output of the program segment?
m:=3;
n:=4;
for i:=1 to m do for j:=1 to n do begin
for k:=1 to (i-1)*n do write(' ');
for k:=1 to j do write('*');
writeln;
end;
A.
*
**
***
****
* **
***
****
* **
***
****
B.
*
**
***
****
*
**
***
****
*
**
***
****
C.
*
**
***
* **
***
* **
***
* **
***
D.
*
**
***
*
**
***
*
**
***
*
**
***
25. Below shows 6 integers arranged in a rectangular form:
3 1 5
4 2 6
You can draw a path from the top left corner, passing through some numbers and ending at the bottom right corner. A path can only extend either to right or downward. For each path, we multiply all the integers along it. What is the maximum and minimum product?
A. Maximum = 72 Minimum = 18 B. Maximum = 144.
Minimum = 36 C. Maximum = 216
Minimum = 48 D. Maximum = 234
Minimum = 48
26. Which of the following data type cannot represent a negative number?
A. Longint B. Extended C. Single D. Byte
27. What is the output of the following program segment?
var
a:integer;
begin
a:=32765;
writeln(a+10);
end.
A. 32775 B. –32775 C. –32761 D. –8
28. Which of the following statement(s) is/are syntactically correct?
(i) if a>b then writeln(a) else;
(ii) if a<=b then writeln(b);
(iii) if b<a then else writeln(a);
A. (ii) only B. (i), (ii) only C. (i), (iii) only D. (i), (ii), (iii)
29. Which of the following statement(s) is/are syntactically correct and return a true value? (a=10, b=11, c=13, d='11', e='1023')
(i) not (c<=b) or (d<e) and (e<='345') (ii) (a>b) and (c<=b) or not (d=e) and (a>c) (iii) b>a and c>a or (a<c) and (d>e)
A. (i) only B. (ii) only C. (i), (iii) only D. (ii), (iii) only
30. Which of the following statement(s) can generate a random integer ranged between 30 and 59 (including 30 and 59)?
(i) random(30)+30 (ii) random(30)*2
(iii) random(30)+random(30) A. (i) only
B. (i), (ii) only C. (ii), (iii) only D. (i), (ii), (iii)
31. What is the output of the following program segment?
no:=0;
for ch:='A' downto 'C' do inc(no);
writeln(no);
A. 0 B. 1 C. 2 D. 3
32. Which of the following can round a real number to two decimal places.
A. A:= trunc(A*100)/100;
B. A:= round(A*100)/100;
C. A:= trunc(A/100)*100;
D. A:= round(A,2);
33. What is the output of the following program segment?
pi:=3.14159265;
writeln('PI=', pi:2:6);
A. PI=3.141592 B. PI=3.141593 C. PI=3.14 D. PI= 3.14
34. Which of the following choices is equivalent to the statement below?
Mark := 50 + (abs(Mark-50) + (Mark-50)) div 2;
A. if Mark<50 then Mark:=0;
B. if Mark<50 then Mark:=0 else Mark:=(Mark div 2)*2;
C. if Mark<50 then Mark:=50 + Mark mod 2;
D. if Mark<50 then Mark:=50;
35. What is the value of B after executing the following statement, assuming B has been declared as a boolean variable?
B := B = B;
A. B remains unchanged B. It always sets B to FALSE C. It always sets B to TRUE D. It always changes the value of B
36. What is the output of the following program segment?
m:=9;
n:=9;
m:=m+n;
n:=m-n;
m:=m+n;
writeln(m,' ',n);
A. 0 9 B. 9 9 C. 18 9 D. 27 9
37. How many '1's will be printed during the execution of the following program segment?
var
i,j,k,l:integer;
begin
for i:=1 to 3 do for j:=1 to 3 do for k:=1 to 3 do;
for l:=1 to 3 do write(1);
end.
A. 3 B. 9 C. 27 D. 81
38. What is the output of the following program segment?
x:=-2;
y:=5;
if (x<0) and (y>3) then x:=x–2
else
if (x<0)or(y>3) then x := x – y ;
write(x);
A. 0 B. –2 C. –3 D. –4
39. What is the output of the following program segment?
a:=78;
a:=a div 10;
case a of
10, 9, 8: writeln(a);
7: writeln(a+1);
6: writeln(a*3) else
writeln(a-2);
end;
A. 7 B. 8 C. 78 D. 79
40. Given a function Power to compute the nth power of x recursively. The value of power(3.0, 4) is 81 and the value of power(2.0, 3) is 8.
The function Power is defined as follow:
function Power(x : real; n : integer) : real;
begin
if n = 0 then Power := 1 else
Power := x * missing;
end;
What should be filled in missing in the above function?
A. Power(x, n) B. Power(x, n+1) C. Power(x-1, n) D. Power(x, n-1)