• 沒有找到結果。

Section A1 (5 marks) For each question, determine whether the statement is true or false, then put down

N/A
N/A
Protected

Academic year: 2021

Share "Section A1 (5 marks) For each question, determine whether the statement is true or false, then put down"

Copied!
15
0
0

加載中.... (立即查看全文)

全文

(1)

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 7 (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. The random seek time of a solid state drive (SSD) is slower than that of a traditional magnetic disk drive.

2. An array of char can be sorted and then perform binary search on it.

3. The range of a double precision floating point (double/double) variable is larger than the range of a single precision floating point variable (single/float).

4. Command "break" can be used in a for-loop but not in a while-loop.

5. Syntax errors can be detected during compilation.

(2)

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. Palindromes are strings that read the same forward or reversed. For example, "abcba"

is a palindrome while "accda" isn't.

Substring of a string is any continuous segment of the string, For example, Both "a", "b",

"c", "ab", "bc" and "abc" are substrings of "abc", while "ac" isn't.

How many strings of length exactly 4 does not consist of any substring that is palindrome and with length larger than 1? Consider characters from "a" to "z" only.

For example, "abcd" is one of the strings that match the condition, while "aabc" is not. (Since

"aa" is a palindrome).

A. 374400 B. 390000 C. 405600 D. 422500

7. Alice has just learnt to use file compressing tools in today's ICT lesson. She compressed an image flower.jpg (2MB) to flower.zip (1MB). Now, she is compressing flower.zip to flower.zip.zip. What is the most appropriate prediction of the size of the double compressed file?

A. 0MB B. 0.5MB C. 1MB D. 2MB

(3)

8. What is the output of the following program?

Pascal Version var

a: array[0..6] of longint = (3, 7, 2, 5, 4, 6, 1);

i, tmp, finished, counter: longint;

begin

finished := 0;

counter := 0;

while finished = 0 do begin

finished := 1;

for i := 0 to 5 do

if a[i] > a[i + 1] then begin

tmp := a[i];

a[i] := a[i + 1];

a[i + 1] := tmp;

finished := 0;

counter := counter + 1;

end end;

write(counter) end.

C Version

#include <stdio.h>

int a[7] = {3, 7, 2, 5, 4, 6, 1};

int i, tmp, finished, counter;

int main() { finished = 0;

counter = 0;

while (finished == 0) { finished = 1;

for (i = 0; i <= 5; i++) if (a[i] > a[i + 1]) { tmp = a[i];

a[i] = a[i + 1];

a[i + 1] = tmp;

counter++;

finished = 0;

} }

printf("%d", counter);

return 0;

}

A. 10 B. 11 C. 12 D. 13

9. We have a 4 x 4 chessboard. A white or black chess must be placed in each cell. Two cells which share a common edge are considered as neighbors.

Every chess must have an odd number of neighboring chess of the same color. For example, the following is a valid configuration.

oxxx ooxo oxoo xxxo

In how many valid ways can you fill the following chessboard? ("x "﹑"o"﹑"." represent black chess, white chess and a cell to be filled respectively)

x.oo ....

....

....

A. 1 B. 2 C. 8 D. 16

(4)

10. In the following program, how many "#"s are printed?

Pascal Version var

i, j: longint;

begin

for i := 1 to 9 do for j := 1 to 9-i do if ((i and j) = 0) then write('#');

end.

C Version

#include <stdio.h>

int main() { int i, j;

for (i = 1; i <= 9; i++) for (j = 1; j <= 9-i; j++) if ((i & j) == 0)

printf("#");

return 0;

}

A. 11 B. 12 C. 13 D. 14

11. What is the output of the following program?

Pascal Version var

ans, i, j: longint;

begin ans := 0;

for i := 1 to 2003 do begin

j := i;

while j > 0 do begin

ans := ans + j mod 10;

j := j div 10;

end;

end;

write(ans);

end.

C Version

#include <stdio.h>

int ans, i, j;

int main() { ans = 0;

for(i = 1; i <= 2003; i++){

j = i;

while(j > 0){

ans = ans + j % 10;

j = j / 10;

} }

printf("%d", ans);

return 0;

}

A. 13510 B. 14510 C. 27014 D. 28014

(5)

For questions 12 to 13, consider the following situation:

Alice and Bob is playing a two-player game called X, in which player takes turn to make a move until one player makes the winning move. They have just discovered that the first player (the player who makes the first move) could guarantee winning no matter how the second player reacts.

They get bored so they add the following rules to the game.

- The game will be repeated for N rounds. (Each round is identical to the original version of X)

- Alice will be first player in round 1.

- The loser of the kth round (1 ≤ k < N) will be the first player in the (k+1) th round.

- The winner of the Nth round will be the winner of the whole game.

12. Given that both players do not want to lose. In which of the following situation(s), Alice will win?

N = i. 5 ii. 10 iii. 13

A. ii only B. i and iii only

C. Cannot be determined D. All of the above

13. After they thoroughly studied the game, they figured out that in each round, the first player could guarantee losing no matter how the second player reacts, too.

Given that both players do not want to lose. In which of the following situation(s), Alice will win?

N = i. 5 ii. 10 iii. 13

A. ii only B. i and iii only

C. Cannot be determined D. All of the above

(6)

14. For integers a, b, c and d, it is given that:

a > b d < b b < c

Which of the following relation(s) can be deduced?

i. a > c ii. a < c iii. c > d iv. d < a

A. i and iv only B. iii and iv only C. iii only

D. ii, iii and iv only

15. There is a machine X in the office, which is invented to store N paper document securely. Each document is stored in an independent cell inside the machine. We will denote the leftmost cell as 1st cell and the rightmost cell as Nth cell.

Following is the internal view of machine X when there are 5 documents for storing, in this case, the pth document is stored in the pth cell. When a staff request access for the document inside kth cell (not necessarily the kth document), X will first move the document to the nearest end of the machine for access and return it to the original cell after it is done. After that, min(N - k + 1, k) unit energy is consumed. (min(a, b) will return the minimum value among a and b)

Using the picture above as an example, when k = 2, X will use 2 unit energy to move the document to the left end and return it.

When k = 5, X will use 1 unit energy to move the document to the right end and return it.

The inventor of X has recorded the frequency of usage of each document per day

Document 1 2 3 4 5

Frequency 4 2 5 1 6

Assuming the above frequency is constant every day, if he could rearrange the document order inside X, what is the minimum energy unit usage per day?

A. 26 B. 27 C. 28 D. 29

(7)

16. Dr. Jones has invented a smart freezer. The freezer takes two integers t and p as input, where t (-273 ≤ t ≤ 1000) is the initial temperature of the food and p (1 ≤ p ≤ 200) is the power setting.

Then, the freezer will repetitively lower the temperature of the food by p degree(s) until the temperature becomes zero or negative. After that, the freezer will turn itself off to save energy.

Which of the following program segments can be plugged into Program Segment A (as labelled in the program) so that the program calculates and outputs the final temperature of the food?

Pascal Version var

i, t, p: longint;

begin

read(t, p);

//Program Segment A write(t);

end.

A.

while (t > p) do t := t - p;

B.

t := t mod p;

C.

t := t * -1;

while (t < -1 * p) do t := t + p;

if t < 0 then t := t + p;

t := t * -1;

D.

t := p div t - (t mod p);

C Version

#include <stdio.h>

int i, t, p;

int main() {

scanf("%d %d", &t, &p);

//Program Segment A printf("%d", t);

return 0;

} A.

while (t > p) t = t - p;

B.

t = t % p;

C.

t = t * -1;

while (t < -1 * p) t = t + p;

if(t < 0) t = t + p;

t = t * -1;

D.

t = p / t - (t % p);

17. What is the output of the following program?

Pascal Version var

tmp, cnt, i: longint;

begin tmp := 0;

cnt := 0;

i := 1;

while (i < 100) do begin

tmp := tmp * i;

tmp := tmp + i;

tmp := tmp mod 1000;

if (tmp mod 5 = 0) then cnt := cnt + 1;

i := i + 1;

end;

write(cnt);

end.

C Version

#include <stdio.h>

int tmp, cnt, i;

int main() { tmp = 0;

cnt = 0;

i = 1;

while (i < 100) { tmp = tmp * i;

tmp = tmp + i;

tmp = tmp % 1000;

if (tmp % 5 == 0) cnt = cnt + 1;

i = i + 1;

}

printf("%d", cnt);

return 0;

} A. 20

B. 29 C. 39 D. 40

(8)

18. What is the output of the following program?

Pascal Version var

k, x, y, tx, ty :longint;

a: array[0..2, 0..4] of longint = ((11, 3, 7, 15, 1), (16, 62, 53, 44, 37), (10, 12, 11, 31, 22)) ;

begin k := 127;

x := 1;

tx := 1;

y := 0;

ty := 0;

while k>0 do begin

dec(k);

x := (a[tx][ty]+tx) mod 3;

y := (a[tx][ty]+ty) mod 5;

tx := x;

ty := y;

end;

write(x, ' ', y);

end.

C Version

#include <stdio.h>

int k, x, y, tx, ty, a[3][5] = {{11, 3, 7, 15, 1}, {16, 62, 53, 44, 37}, {10, 12, 11, 31, 22}};

int main(){

k = 127;

x = 1;

tx = 1;

y = 0;

ty = 0;

while(k>0){

k--;

x = (a[tx][ty] + tx) % 3;

y = (a[tx][ty] + ty) % 5;

tx = x;

ty = y;

}

printf("%d %d", x, y);

return 0;

} A. 0 4

B. 0 3 C. 1 3 D. 2 3

19. Alice will be happy if and only if she eat an ice cream or a salad (she won't feel happy if she eat both), but she will donate dollars to the charity if she eat an ice cream and a salad.

Which of the following may be correct?

i. Alice did not feel happy and donated dollars to the charity ii. Alice felt happy and donated dollars to the charity

A. None of the above B. i only

C. ii only D. i and ii

20. Variables a, b are integers. Which of the following expression(s) is/are equivalent to a > b?

i. a - b > 0 ii. a + 1 > b + 1 iii. a * 1 > b * 1 iv. a * -1 < b * -1

A iii only B. i and ii only C. iii and iv only D. All of the above

(9)

21. Consider a robot on an Cartesian plane. Initially, it is located at (0, 0). In each step, it will walk 1 unit to north (y+), east (x+) or west (x-) with equal probability.

Which of the following cells could be reached in the 7th step of the robot?

i. (0, 0) ii. (0, 1) iii. (1, 1) iv. (2, 1) v. (2, 2) A. i, ii and iii only B. ii, and iv only C. i, iii and iv only D. All of the above

22. Refer to Question 21, if we extend the plane to 3-dimensional space, and initially the robot is located at (0, 0, 0). In each step, the robot will walk 1 unit north (y+), east (x+), west (x-) or upwards (z+) with equal probability.

Which of the following cells could be reached in the 7th step of the robot?

i. (0, 0, 1) ii. (0, 1, 2) iii. (1, 2, 3) A. i and ii only B. ii and iii only C. i and iii only D. All of the above

23. What is the output of the following program?

Pascal Version var

i, n: longint;

s: string;

procedure flip(x: longint);

var i: longint;

begin

for i := x to n do if s[x] = s[i] then

s[i] := chr(ord('Z') - ord(s[i]) + ord('A'))

end;

begin

s := 'ABAAZYX';

n := length(s);

for i := 1 to n do flip(i);

write(s) end.

C Version

#include <stdio.h>

#include <string.h>

int i, n;

char s[] = "ABAAZYX";

void flip(int x){

for(int i = x; i < n; i++){

if(s[x] == s[i])

s[i] = 'Z' - s[i] + 'A';

} }

int main(){

n = strlen(s);

for(i = 0; i < n; i++) flip(i);

printf("%s", s);

return 0;

} A. ZYZZZYC

B. ZYZZABC C. ZYAZABC D. ZYAAZBC

(10)

24. Below we will use the boolean operator NOR.

For any boolean variables p and q, p NOR q = NOT(p OR q)

a, b, c, d, e are five boolean variables. Given that (a OR b) NOR ((c OR d) AND e) is True, which of the following expressions must be True?

A. c NOR e B. a NOR e C. a NOR b D. b NOR c

25. Which of the following program segment(s) will output OIOIOIOI?

Pascal Version C Version

i

for i := 1 to 4 do write('OI');

for (i = 1; i <= 4; i++) printf("OI");

ii

for i := 1 to 8 do if (i mod 2 = 0) then write('O')

else

write('I');

for (i = 1; i <= 8; i++) if (i % 2 == 0)

printf("O");

else

printf("I");

iii

for i := 1 to 4 do write('O');

write('I');

for (i = 1; i <= 4; i++) printf("O");

printf("I");

A. i only B. ii only C. i and ii only D. i and iii only

END OF SECTION A

(11)

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. Given that function datecmp is returning 1 (Pascal: true) if date 1 (d1, m1, y1) is earlier than date 2 (d2, m2, y2), returning 0 (Pascal: false) if otherwise. Please complete the blank in the following program.

The function datecmp satisfies:

1 ≤ d1, d2 ≤ 31, 1 ≤ m1, m2 ≤ 12 and 1 ≤ y1, y2 ≤ 9999

Pascal Version

function datehash(d, m, y:longint):longint;

begin

datehash := A ; end;

function datecmp(d1, m1, y1, d2, m2, y2:longint):boolean;

begin

datecmp := datehash(d1, m1, y1) <

datehash(d2, m2, y2);

end;

C Version

int datehash(int d, int m, int y) { return A ;

}

int datecmp(int d1, int m1, int y1, int d2, int m2, int y2) {

return datehash(d1, m1, y1) <

datehash(d2, m2, y2);

}

Answer: A (2 marks)

2. The NAND(not-and) function has many applications. The truth table of NAND, with two inputs, is as follows

A B A NAND B T T F

T F T F T T F F T

Let's use Q to represent the NAND function. Therefore the NAND of X and Y can be represented as follows: (XQY), and the result is also a boolean expression. Also A and B themselves are boolean expressions. For example, the NOT function of A can be expressed as (AQA).

Please note that brackets in the above example are compulsory, no mark will be given for answers that miss brackets in the following questions.

(12)

Given two inputs A and B, represent the following truth table using boolean expressions with variables A, B, brackets and Q (NAND) only.

A B Result T T T T F F F T F F F F

Answer: B (2 marks)

3. There are some ice cubes floating. The rectangles represent the cubes and the narrow gaps between rectangles mean that you can walk from between the two ice cubes. Alice is on one of the ice cubes. As ice cubes keep on melting, she needs to move from one ice cube to another ice cube.

Once she leaves an ice cube, the ice cube melts and Alice can no longer stand on that ice cube.

Eventually she will be trapped on one of the ice cube having no way to go. Bob, who is the enemy of Alice, would like to know all the possibilities of Alice's position when she will be trapped.

Please write down the index(es) of that/those ice cube(s).

Answer: C (2 marks)

(13)

4. The following program reads an integer n and outputs a (2n+1) x (2n+1) grid.

Pascal Version var

n, i, j: longint;

begin read(n);

for i := 0 to 2*n do begin

for j := 0 to 2*n do if ( D, E ) then write('*')

else

write(' ');

writeln();

end;

end.

C Version

#include <stdio.h>

int main() { int n, i, j;

scanf("%d", &n);

for (i = 0; i <= 2*n; i++) { for (j = 0; j <= 2*n; j++) { if ( D, E )

printf("*");

else

printf(" ");

}

printf("\n");

}

return 0;

}

If the input n is 4, complete the program such that the output is

* * * * * * * * * * * * * * *

* *

Answer: D (2 marks)

If the input n is 5, complete the program such that the output is

***********

***********

***********

***********

***********

***********

***********

***********

***********

***********

***********

Answer: E (2 marks)

(14)

5. Let f(a, b, c) be a function that can gives the minimum of the three integers a, b, c.

For example, f(2, 1, 3) = 1. Answer F and G using this function.

Write an expression which gives the maximum of three positive integers x, y and z.

Answer: F (1 mark)

Let x, y, z be 3 integers (1 ≤ x, y, z ≤ 100). Procedure isTri determines if a valid triangle can be formed with sides of length x, y and z. Please complete the procedure by using f(a, b, c).

Pascal Version

procedure isTri(x, y, z: longint);

begin

if (_____G_____) then write('Valid triangle') else

write('Cannot form a triangle') end.

C Version

void isTri(int x, int y, int z) { if (_____G_____)

printf("Valid triangle");

else

printf("Cannot form a triangle");

} Answer: G (2 marks)

6. The goal of the following program segment is to factorize a positive integer n into product of prime numbers. However, one of the lines is incorrect. Please find it and correct it.

Pascal Version 10 k := 2;

11 while (k <= n) do 12 begin

13 if (n mod k = 0) then 14 begin

15 n := n div k;

16 if (n > 1) then 17 write(k, ' * ') 18 else

19 write(k) 20 end;

21 k := k+1;

22 end;

C Version 50 k = 2;

51 while (k <= n) { 52 if (n%k == 0) { 53 n = n/k;

54 if (n > 1)

55 printf("%d * ", k);

56 else

57 printf("%d", k);

58 } 59 k = k+1;

60 }

Line number: H (1 mark) Correction: I (1 mark)

(15)

7. We define numbers which have the following properties as "Lucky number".

- The number contains exactly 5 digits.

- The number does not contain any "0" as a digit.

- The first, third and the fifth digits are odd while the second and the forth digits are even .

For example, 12345 and 54545 are "Lucky Number" while 12305 and 12344 are not.

We call the "Lucky Number" with smallest lexicographical order (which is 12121) the 1st "Lucky number" the "Lucky Number" with second smallest lexicographical order (which is 12123) is the 2nd "Lucky number", and so on.

Calculate how many different "Lucky number" are there: J (1 mark) Write down the 1234th "Lucky number": K (2 marks)

Consider the following program segment. Let a be an integer array which a[i] stores the ith digit of a 5-digit number. (The 1st digit of 12345 is 1)

Pascal Version

if ((a[2] > 0) and (a[4] > 0) and (a[2]

mod 2 = 0) and (a[4] mod 2 = 0) and ( L )) then

write('It is a Lucky number') else

write('It is not a Lucky number');

C Version

if (a[2] > 0 && a[4] > 0 && a[2] % 2 ==

0 && a[4] % 2 == 0 && L ) printf("It is a Lucky number");

else

printf("It is not a Lucky number");

The above program segment is used for checking whether an integer is a "Lucky number". Please complete it.

Answer: L (2 marks)

END OF PAPER

參考文獻

相關文件

We do it by reducing the first order system to a vectorial Schr¨ odinger type equation containing conductivity coefficient in matrix potential coefficient as in [3], [13] and use

(6) (a) In addition to the powers of the Permanent Secretary under subsection (1) the Chief Executive in Council may order the Permanent Secretary to refuse to

• Content demands – Awareness that in different countries the weather is different and we need to wear different clothes / also culture. impacts on the clothing

• Examples of items NOT recognised for fee calculation*: staff gathering/ welfare/ meal allowances, expenses related to event celebrations without student participation,

Study the following statements. Put a “T” in the box if the statement is true and a “F” if the statement is false. Only alcohol is used to fill the bulb of a thermometer. An

We propose a primal-dual continuation approach for the capacitated multi- facility Weber problem (CMFWP) based on its nonlinear second-order cone program (SOCP) reformulation.. The

Like the proximal point algorithm using D-function [5, 8], we under some mild assumptions es- tablish the global convergence of the algorithm expressed in terms of function values,

support vector machine, ε-insensitive loss function, ε-smooth support vector regression, smoothing Newton algorithm..