HKOI2014 Heat Event (Junior Group) Assume that all variables without declaration shown in the following program segments have already been declared properly. Integers in problem statements are 32-bit signed variables (Pascal: longint, C: int). Assume all the programs are compiled properly without using any compiler flag (except the "-o" option in C).
Format # Questions Total Marks
Section A1 True or False 5 5
Section A2 Multiple Choice 20 20
Section B Fill-in-the-blanks 5 (A-L) 20
Total 45
Section A1 (5 marks)
For each question, determine whether the statement is true or false, then put down T or F in the corresponding space on the answer sheet. One mark for each correct answer. No marks will be deducted for wrong answers
1. Data transfer faster in optic fibre than copper wire because electromagnetic waves travel faster in optic fibre than in copper.
2. The ASCII code of 'A' is smaller than that of 'a'.
3. A variable of data type short (Pascal: smallint) can store the value -12345.
4. A variable can be named 123abc or 789def but not 012abc.
5. It takes the same time to execute each line of code in a program.
Section A2 (20 marks)
For each question, choose the most appropriate answer and write the letter (A, B, C or D) in the corresponding space on the answer sheet. One mark for each correct answer. No marks will be deducted for wrong answers.
6. In chess, "Bishop" can attack in 4 diagonal directions. On a 7x7 chessboard, at least how many bishops do you need to place so that any square on the chessboard can be attacked by at least one bishop?
A. 5 B. 7 C. 14 D. 25
7. What is the output of the following program?
Pascal Version var
i: longint;
begin
for i := 1 to 10 do begin
if (i mod 3 = 0) then begin
write(i);
continue end
end end.
C Version
#include <stdio.h>
int main() { int i;
for (i = 1; i <= 10; i++) { if (i%3 == 0) {
printf("%d", i);
continue;
} }
return 0;
}
A. 3 B. 123 C. 369 D. 12457810
8. Which control flow does the diagram on the right represent?
A. If-then B. If-then-else C. For loop D. While loop
HKOI2014 Heat Event (Junior Group) 9. Consider the following program:
Pascal Version var n: longint;
s: string;
begin n := 3;
s := 'hkoi';
1. write(n*s);
2. write(s*n) end.
C Version
#include <stdio.h>
int main() { int n = 3;
char s[] = "hkoi";
1. printf("%s", n*s);
2. printf("%s", s*n);
return 0;
}
Which of the following statements is true?
A. The program compiles successfully and the output is hkoihkoihkoihkoihkoihkoi.
B. The program cannot compile successfully unless line 1 is removed. Subsequently, the output would be hkoihkoihkoi.
C. The program cannot compile successfully unless line 2 is removed, Subsequently, the output would be hkoihkoihkoi.
D. The program cannot compile successfully unless both lines 1 and 2 are removed.
10. The following is the truth table for operator ◊:
A B A ◊ B
True True True
True False True
False True True
False False False
Simplify the following boolean expression, where U and V are boolean variables (NOT U) ◊ (U ◊ V)
A. True B. False C. U D. V
11. There are many people in a party. Some pairs of people are friends. Their relationship is interesting. If A and B are friends, and B and C are friends, then A and C would be friends too. You are given the following information about the people in the party:
Tryndamere and Taric are friends.
Master Yi and Wukong are friends.
Xin Zhao and Jarvan IV are friends.
Lee Sin and Garen are friends.
Taric and Xin Zhao are friends.
At least how many pairs of friends are there?
A. 5 B. 6 C. 7 D. 8
12. What is the name of the sorting algorithm used in the program below?
Pascal Version var
a:array[0..4] of longint;
i, j, k:longint;
begin
read(a[0], a[1], a[2], a[3], a[4]);
for i := 0 to 4 do begin
j := a[i];
k := i-1;
while (k >= 0) do begin
if (a[k] < j) then break;
a[k+1] := a[k];
k := k-1;
end;
a[k+1] := j;
end end.
C Version
#include <stdio.h>
int a[5];
int i, j, k;
int main() {
scanf("%d%d%d%d%d", &a[0], &a[1], &a[2],
&a[3], &a[4]);
for (i = 0; i < 5; i++) { j = a[i];
k = i-1;
while (k >= 0) {
if (a[k] < j) break;
a[k+1] = a[k];
k = k-1;
}
a[k+1] = j;
}
return 0;
} A. Regular sort
B. Bubble sort C. Insertion sort D. Selection sort
HKOI2014 Heat Event (Junior Group) 13. Evaluate the expression.
Pascal Version
72 div 12 div 6 mod 4
C Version
72 / 12 / 6 % 4 A. 1
B. 3 C. 6 D. 12
14. Tom knows that:
He will get full marks in the exam if and only if he studied hard the night before the exam.
He will have a nice meal if and only if he get full marks in the exam.
He will feel happy if and only if he get full marks in the exam.
He will not play computer games if and only if he is not happy.
Now Tom feels happy. Which of the following may not be true?
A. He studied hard the night before the exam B. He got full marks in the exam
C. He had a nice meal
D. He will not play computer games
15. What is the output of the following program segment?
Pascal Version
write(12 and 4, ' ', 10 or 21);
C Version
printf("%d %d", 12 & 4, 10 | 21);
A. 4 0 B. 4 31 C. 12 1 D. 12 10
16. Which of the following data types can be used as the control variable in a switch (case-of) statement?
i. char
ii. double (Pascal: real)
A. None of the two types B. i only
C. ii only D. i and ii
17. A queue is a data structure that supports two kinds of operation, namely:
Dequeue(): Remove the earliest element that has not been removed from the queue and return the element.
Enqueue(x): Insert an element x into end of the queue What is the output of the following pseudocode?
Enqueue(3) Enqueue(4) Enqueue(5) Enqueue(6) Dequeue() Dequeue()
Enqueue(Dequeue()) Enqueue(7)
Dequeue()
Output Dequeue()
A. 4 B. 5 C. 6 D. 7
18. There are infinitely many $1 and $2 coins. You can pick up the coins one by one until you have collected exactly $6. How many different ways of picking are there? (The order matters)
A. 4 B. 5 C. 8 D. 13
19. Consider the following program segment:
Pascal Version read(x);
if (x <= 0) then begin
write(x);
end else begin
while (x < 8) do begin
x := x - 1;
while ((x mod 2) = 0) do x := x + 3;
end;
write(x);
end;
C Version
scanf("%d", &x);
if (x <= 0){
printf("%d", x);
} else {
while (x < 8){
x--;
while (x%2 == 0){
x = x+3;
} }
printf("%d", x);
}
HKOI2014 Heat Event (Junior Group)
If input x = 3, what is the output?
A. 3 B. 9 C. 10 D. 11
20. What is the output of the following program?
Pascal Version var
i, j:longint;
a:array[0..8] of longint = (2, 0, 3, 7, 5, 8, 6, 4, 2);
begin j := 1;
for i := 0 to 6 do j := a[j];
write(a[j]);
end.
C Version
#include <stdio.h>
int i, j;
int a[9] = {2, 0, 3, 7, 5, 8, 6, 4, 2};
int main() { j = 1;
for (i = 0; i <= 6; i++) { j = a[j];
}
printf("%d", a[j]);
return 0;
}
A. 2 B. 0 C. 8 D. 5
21. What is the output of the following program?
Pascal Version var
i, j:longint;
a:array[0..8] of longint = (2, 0, 3, 7, 5, 8, 6, 4, 2);
begin j := 0;
for i := 0 to 10007 do j := a[j];
write(a[j]);
end.
C Version
#include <stdio.h>
int i, j;
int a[9] = {2, 0, 3, 7, 5, 8, 6, 4, 2};
int main() { j = 0;
for (i = 0; i <= 10007; i++) { j = a[j];
}
printf("%d", a[j]);
return 0;
} A. 2
B. 8 C. 0 D. 3
22. Consider the following program segment:
Pascal Version var x:longint;
...
x := 32768;
if (x*x*2 > x) then write('true') else
write('false');
C Version int x;
...
x = 32768;
if (x*x*2 > x) printf("true");
else
printf("false");
Which of the following statements is true?
A. The output is false. B. The output is true.
C. The program segment causes runtime error.
D. The program segment causes compilation error.
23. Consider the following program segment. The variables a, b and c are declared as certain data types.
Pascal Version b := a;
c := b;
if (a = c) then write('Yes') else
write('No');
C Version b = a;
c = b;
if (a == c)
printf("Yes");
else
printf("No");
Which of the following statements is true?
A. If a and c are declared as 32-bit signed integers, and b is declared as 16-bit signed integer, then the output is always "Yes" for any initial value of a.
B. If a and c are declared as 32-bit signed integers, and b is declared as 16-bit signed integer, then the output is always "No" for any initial value of a.
C. If a and c are declared as 16-bit signed integers, and b is declared as 32-bit signed integer, then the output is always "Yes" for any initial value of a.
D. If a and c are declared as 16-bit signed integers, and b is declared as 32-bit signed integer, then the output is always "No" for any initial value of a.
HKOI2014 Heat Event (Junior Group) For questions 24 to 25, consider the following program segment:
Pascal Version [I]
begin
write('1');
[J]
begin [K]
begin
write('3') end;
write('2') end
end
C Version [I] {
printf("1");
[J] { [K] {
printf("3");
}
printf("2");
} }
We would fill in each space ([I], [J] and [K]) by one of the following lines:
Pascal Version
L: for i := 0 to 1 do M: for j := 0 to 2 do N: for k := 0 to 3 do
C Version
L: for (i = 0; i <= 1; i++) M: for (j = 0; j <= 2; j++) N: for (k = 0; k <= 3; k++)
24. Which of the following assignments produces the output
"133323332133323332133323332133323332"?
[I] [J] [K]
A. L M N
B. M N L
C. N L M
D. N M L
25. Which assignment produces the smallest output? (Treat the output as a decimal number.) [I] [J] [K]
A. L M N
B. M N L
C. N L M
D. N M L
END OF SECTION A
Section B (20 marks)
The blanks are labeled from A to L. Please fill in the blanks on the answer sheet.
Except otherwise specified, two marks for each correct blank. No marks will be deducted for wrong answers.
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.
(3) You can write only one character in each box on the answer sheet.
(4) No answer with length greater than the designated number of boxes will be accepted.
1. You want to carry some items from one city to another. There are two types of goods that you can choose:
Rice - each bag weighs 2 kg and can be sold for $6.
Cabbage - each pack weighs 3 kg and can be sold for $10.
What is the maximum dollar amount you can get after selling the goods, assuming that you can only carry not more than 16 kg of goods?
Answer: $ A (2 marks)
2. Assume n = 1, 2, 3 or 4. Function f(n) computes the n-th prime number. Complete the function.
Pascal Version
function f(n:longint):longint;
begin
if ( B1 ) then B2 ; else f := C ;
end;
C Version
int f(int n) {
if ( B1 ) B2 ; else return C ; }
B1 and B2: 2 marks if both correct C: 2 marks
HKOI2014 Heat Event (Junior Group) 3. Consider the following program:
Pascal Version var
n, a, i, s:longint;
begin read(n);
s := 0;
for i := 1 to n do begin
read(a);
if (a > 0) then s := s+a;
end;
write(s/n:0:3);
end.
C Version
#include <stdio.h>
int n, a, i, s;
int main() { scanf("%d", &n);
s = 0;
for (i = 1; i <= n; i++) { scanf("%d", &a);
if (a > 0) s = s+a;
}
printf("%.3f", (double)s/n);
return 0;
}
What is the output of the program when given the following input?
8
6 3 -2 9 13 1 2 5
Answer: D (2 marks)
What should X and Y be if the program outputs 32.500 given the following input? Write down one possible combination.
4
100 200 X Y
X: E1 , Y: E2 (2 marks if both correct)
4. Consider the following program:
Pascal Version 11 var
12 i, n:longint;
13 begin
14 write('Input integer n: ');
15 read(n);
16 for i := 1 to 45 do 17 begin
18 if (i*(i+1) = n*2) then 19 begin
20 write('n is a triangular number');
21 end 22 end;
23 write('n is not a triangular number');
24 end.
C Version
51 #include <stdio.h>
52 int n, i;
53 int main() {
54 printf("Input integer n: ");
55 scanf("%d", &n);
56 for (i = 1; i <= 45; i++) { 57 if (i*(i+1) == n*2) {
58 printf("n is a triangular number");
59 } 60 }
61 printf("n is not a triangular number");
62 return 0;
63 }
The above program determines whether input n is a triangular number. You may assume that n is an integer in [1, 1000]. However there is one error. Determine the type of error (compilation, run-time or logic), line number and then correct it.
Type of error: F (1 mark, check ☑ the appropriate answer) Line number: G (1 mark)
Correct the line: H (2 marks)
HKOI2014 Heat Event (Junior Group) 5. Assume function/procedure swap(i, j) swaps the values of a[i] and a[j].
Say if a[3] = 5 and a[6] = 1, after swap(3, 6), a[3] becomes 1 and a[6] becomes 5.
Complete the following program segment so that it sorts array a[9] (Pascal: a[0..8]) in ascending order.
Pascal Version p := 0;
while (p < 8) do begin
if a[p] > a[p+1] then begin
I ;
if ( J1 ) then J2 ; end
else begin
K ; end
end;
C Version p = 0;
while (p < 8) {
if (a[p] > a[p+1]) { I ;
if ( J1 ) J2 ; } else {
K ; }
}
I: 1 mark J1 and J2: 2 marks if both correct K: 1 mark
Now, complete the following implementation of function/procedure swap(i, j). Pascal Version
procedure swap(i:longint; j:longint);
begin
a[i] := a[i] + a[j];
L ;
a[i] := a[i] - a[j];
end;
C Version
void swap(int i, int j) { a[i] = a[i] + a[j];
L ;
a[i] = a[i] - a[j];
}
L: 2 marks
END OF PAPER