• 沒有找到結果。

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!
12
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 6 (A-K) 22

Total 47

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 performance of cloud computing is reduced in a sunny day without cloud.

2. Overflow may occur when you add two negative integers.

3. Given n is an integer, Pascal: n div 2, C: n/2 is always less than or equal to n/2.0

4. There exists N such that there are more even numbers than odd numbers in the first N numbers in the Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, ...

5. break; and continue; are both valid statements in all while loop, do-while loop and for loops.

(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. Tic-tac-toe is a 2-player game. Each player takes turn to draw 'O' or 'X' onto a 3x3 grid. The first player chooses an empty cell to draw a 'O', then the second player chooses another empty grid cell to draw a 'X', and so on, until all cells are filled. A player wins if he can draw 3 same symbols ('O' or 'X') along any horizontal, vertical or diagonal line. If both players fail to do so, the game ends in a draw.

Given that both players do not want to lose. Which of the following situation(s) will turn out to end in a draw?

i.

X O

ii.

X O

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

D. None of the above

7. A stands in front of B, B stands in front of C, C stands in front of D.

The distance between A and C is less than or equal to 5 meters.

The distance between B and D is less than or equal to 9 meters.

The distance between B and C is greater than or equal to 3 meters.

What is the greatest possible distance between A and D?

A. 7 B. 8 C. 11 D. 17

8. Given x and y are non-negative integers, what is the largest integer that cannot be obtained from 3x+7y?

A. 5 B. 7 C. 11 D. 13

(3)

9. What is the output of the following program?

Pascal Version begin

write('H');//write('K');

//write('O'); write('I');

write('2');//write('0');

//write('1'); write('3');

end.

C Version

#include <stdio.h>

int main(){

printf("H");//printf("K");

//printf("O"); printf("I");

printf("2");//printf("0");

//printf("1"); printf("3");

return 0;

}

A. H2 B. HI23 C. HK20 D. HKOI2013

10. What is the return value of function f()? Pascal Version

function f:longint;

var i,k:longint;

begin k := 2;

for i:=1 to 2013 do if (i mod k = 0) then k := k*2;

f := k;

end;

C Version int f() { int i, k = 2;

for (i=1; i<=2013; ++i) if (i % k == 0)

k = k*2;

return k;

}

A. 1024 B. 2048 C. 32768 D. 65536

(4)

11. What is the output of the following program?

Pascal Version var

a:array[0..7] of longint =

(4,7,1,6,2,8,3,5);

x,ans,i,j,t:longint;

begin x:=4;

for i:=0 to x-1 do for j:=1 to 7-i do begin

if (a[j-1] > a[j]) then begin

t := a[j-1];

a[j-1] := a[j];

a[j] := t;

end;

ans:=a[j];

end;

write(ans);

end.

C Version

#include <stdio.h>

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

int x=4, ans=-1, i, j, t;

int main() {

for (i=0; i<x; ++i) for (j=1; j<8-i; ++j) { if (a[j-1] > a[j]) { t = a[j-1];

a[j-1] = a[j];

a[j] = t;

}

ans = a[j];

}

printf("%d", ans);

return 0;

}

A. 3 B. 4 C. 5 D. 6

12. X, Y and Z are specialized at making wineX, wineY and wineZ respectively. The three persons are smart and they have a way to fairly trade their wine among each other. The total value of wine received by one is equal to the value he gave away.

Suppose X kept half of wineX and gave one-fourth of wineX to Y and one-fourth to Z.

Y divided wineY evenly among the three persons, one-third going to each other.

Z gave half of wineZ to X and divided the other half wineZ evenly between Y and himself.

The result is summarized in the following table:

wineX wineY wineZ X 1/2 1/3 1/2 Y 1/4 1/3 1/4 Z 1/4 1/3 1/4

If wineZ is worth 3 thousand dollars. What are the values (in thousand dollars) of wineX and wineY respectively?

A. wineX: 3 wineY: 4 B. wineX: 4 wineY: 3 C. wineX: 5 wineY: 3 D. wineX: 6 wineY: 3

(5)

13. There are 8 booleans, named A, B, ..., H.

A and E have the same value.

D and B have different values.

D and G have different values.

F and G have the same value.

A and B have different values.

C and D have the same value.

F and D have different values.

Which of the following MUST be correct?

A. A and H have the same value.

B. B and G have different values.

C. C and F have the same value.

D. D and E have the same value.

14. What is the output of the following program?

Pascal Version var

s:string = 'hkoi2013';

c:char; i:longint;

begin c:='a';

for i:=1 to 8 do if (s[i] > c) then begin

write(s[i]);

c:=s[i];

end;

end.

C Version

#include <stdio.h>

char s[] = "hkoi2013";

char c = 'a'; int i;

int main() {

for (i=0; i<8; ++i) if (s[i] > c) { printf("%c", s[i]);

c = s[i];

}

return 0;

}

A. hko B. hkoi C. hko23 D. h20

15. Assume you know the following relationships:

i. If it is cloudy today, Ken feels sad.

ii. If Ken feels sad, he will play computer games for a whole day.

iii. If Ken plays computer games for a whole day, he will eat a lot at dinner.

You know that Ken plays computer games for a whole day today, which of the following statements can be TRUE?

A. Today is cloudy.

B. Ken feels sad today.

C. Ken eats a lot at dinner.

D. All of above

(6)

i.

ii.

iii.

i.

ii.

iii.

16. Which of the following code segments will NOT exit?

Pascal Version a:=2013;

while (a>0) inc(a);

b:=2013.0;

while (b<>0.0) b:=b-0.5;

c:=2013.0;

while (c<>0.0) c:=c-0.1;

C Version a=2013;

while (a>0) ++a;

b=2013.0;

while (b!=0.0) b=b-0.5;

c=2013.0;

while (c!=0.0) c=c-0.1;

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

For questions 17-19, consider the following program segment:

Pascal Version

procedure P1(n:longint);

var i,j,k:longint;

begin

for i:=0 to n-1 do for j:=i+1 to n-1 do for k:=j+1 to n-1 do write('*');

end;

procedure P2(n:longint);

var i,j:longint;

begin

for i:=0 to n-1 do for j:=0 to n-1 do write('*');

end;

procedure P3(n:longint);

var i:longint;

begin

if (n >= 1) then begin

for i:=0 to n-1 do write('*');

P3(n div 2);

P3(n div 2);

end;

end;

procedure P4(n:longint);

begin

if (n >= 5) then begin

write('*');

P4(n-1);

P4(n-1);

end;

end;

C Version

void P1(int n){

int i,j,k;

for (i=0; i<n; i++) for (j=i+1; j<n; j++) for (k=j+1; k<n; k++) printf("*");

}

void P2(int n){

int i,j;

for (i=0; i<n; i++) for (j=0; j<n; j++) printf("*");

}

void P3(int n){

int i;

if (n >= 1){

for (i=0; i<n; i++) printf("*");

P3(n/2);

P3(n/2);

} }

void P4(int n){

if (n >= 5){

printf("*");

P4(n-1);

P4(n-1);

} }

(7)

17. Among P1(4),P2(4), P3(4) and P4(4), which of them will print the most number of '*'?

A. P1 B. P2 C. P3 D. P4

18. Among P1(9), P2(9), P3(9) and P4(9), which of them prints the most number of '*'?

A. P1 B. P2 C. P3 D. P4

19. What is the minimum value of n such that P4(n) prints more '*' than all P1(n), P2(n) and P3(n)?

A. 10 B. 11 C. 12

D. Such n does not exist

20. What is the output of the following program?

Pascal Version

var s:string='hkoi2013';

i:longint;

begin

for i:=1 to 4 do begin

s[i]:=s[9-i]; s[9-i]:=s[i];

end;

writeln(s);

end.

C Version

#include <stdio.h>

char s[] = "hkoi2013";

int i;

int main() {

for (i=0; i<4; i++) {

s[i] = s[7-i]; s[7-i] = s[i];

}

printf("%s\n",s);

}

A. hkoi2013 B. 3102iokh C. hkoiiokh D. 31022013

(8)

21. a, b, c are positive integers less than 10000 that represent the side lengths of a triangle.

Which of the following expressions CANNOT determine whether the triangle is valid?

Pascal Version

A. (b>a-c)and(a>c-b)and(a+c>b) B. (a+b>c)and(b+c>a)and(c>b-a) C. (c>a-b)and(a>b-c)and(a>c-b) D. (c>b-a)and(b>c-a)and(a>b-c)

C Version

A. (b>a-c)&&(a>c-b)&&(a+c>b) B. (a+b>c)&&(b+c>a)&&(c>b-a) C. (c>a-b)&&(a>b-c)&&(a>c-b) D. (c>b-a)&&(b>c-a)&&(a>b-c)

22. How many ways are there to fill 2 cells with black such that the two black cells are NOT in the same row?

A. 18 B. 27 C. 36 D. 54

23. What is the output of the following program?

Pascal Version

var ans,i:longint;

function f(n:longint):longint;

var cnt,k:longint;

begin cnt:=0;

k:=1;

while (k<=n) do begin

if (k and n > 0) then cnt:=cnt+1;

k:=k*2;

end;

f:=cnt;

end;

begin ans:=0;

for i:=0 to 15 do ans:=ans+f(i);

writeln(ans);

end.

C Version

#include <stdio.h>

int ans, i;

int f(int n){

int cnt,k;

cnt=0;

k=1;

while (k<=n){

if (k&n > 0) ++cnt;

k*=2;

}

return cnt;

}

int main(){

ans=0;

for (i=0; i<16; i++) ans+=f(i);

printf("%d\n",ans);

return 0;

}

(9)

A. 16 B. 32 C. 48 D. 64

24. (Cancelled)

25. Consider the following function:

Pascal Version

function f(n:longint):longint;

var ans,i:longint;

begin ans:=n;

for i:=2 to n do begin

if (n mod i = 0) then ans:=ans div i*(i-1);

while (n mod i = 0) do n:=n div i;

end;

f:=ans;

end;

C Version int f(int n){

int ans,i;

ans=n;

for (i=2; i<=n; i++){

if (n%i==0)

ans=ans/i*(i-1);

while (n%i==0) n/=i;

}

return ans;

}

Which of the following input n produces the greatest value of f(n)?

A. 53 B. 54 C. 55 D. 56

END OF SECTION A

(10)

Section B (22 marks)

The blanks are labeled from A to K. 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. Let x be the minimum positive multiple of 17 that contains only digit '1'. How many digits does x have?

A

2.

The above figure shows a map of a village with 4 houses.

Houses and roads may be visited/used zero or more times.

Examples of paths of length 3 are H→K→O→I and K→I→O→I.

Please note that H→K→O→I, H→O→K→I and I→O→K→H are considered different.

The total number of different paths of length 3 is B .

3. A rotation of a string is formed by removing zero or more consecutive characters from the left and append them to its right.

For example, cdeab is a rotation of abcde.

Let s = "hkoi2013", write a string q such that every rotation of s is a substring of q.

C

(11)

4. Consider the following program:

Pascal Version 10 var

11 x1,y1,x2,y2:longint;

12 function dist(x1,y1,x2,y2:longint):longint;

13 begin

14 dist:=trunc(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

15 end;

16 begin

17 readln(x1,y1,x2,y2);

18 if (dist(x1,y1,x2,y2)<1) then

19 writeln('The distance is less than 1') 20 else if (dist(x1,y1,x2,y2)=1) then 21 writeln('The distance is 1') 22 else

23 writeln('The distance is greater than 1');

24 end.

C Version

50 #include <stdio.h>

51 #include <math.h>

52 int x1,y1,x2,y2;

53 int dist(int x1, int y1, int x2, int y2) {

54 return floor(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

55 }

56 int main() {

57 scanf("%d %d %d %d",&x1,&y1,&x2,&y2);

58 if (dist(x1,y1,x2,y2)<1)

59 printf("The distance is less than 1\n");

60 else if (dist(x1,y1,x2,y2)==1) 61 printf("The distance is 1\n");

62 else

63 printf("The distance is greater than 1\n");

64 return 0;

65 }

The program checks if the distance between two coordinates (x1,y1), (x2,y2) is less than, equal to, or greater than 1. You may assume that x1,y1,x2 and y2 are integers in [-10000,10000]. Unfortunately, this program contains a logic error but it can be fixed by changing a single line. Identify the line and rewrite it.

Line Number: D Rewrite the line: E

Note for C Version: y1 is defined as double y1(double) in <math.h>. In this question please assume that there is no such definition.

Disregard if you do not understand this note.

(12)

5. Assume array a (Pascal: a[0..n-1], C: a[n]) consists of distinct positive integers and max(i,j) returns the maximum value between a[i] to a[j] inclusive.

Complete the program such that it finds the second maximum value of array a.

Pascal Version

maximum := max(0, n-1);

for i:=0 to n-1 do

if ( F ) then G ;

a[i] := -a[i];

secondmaximum := H ; a[i] := -a[i];

C Version

maximum = max(0, n-1);

for (i=0; i<n; i++) if ( F ) G ; a[i] = -a[i];

secondmaximum = H ; a[i] = -a[i];

6. Observe the following non-standard die.

Now, complete its net on the answer sheet.

1 mark for each correct value. 2 marks for each correct value and orientation.

END OF PAPER

參考文獻

相關文件

circuit sat: Given a circuit, is there a truth assignment such that the circuit outputs true?.. • circuit sat ∈ NP: Guess a truth assignment and then evaluate the

circuit sat: Given a circuit, is there a truth assignment such that the circuit outputs true?. • circuit sat ∈ NP: Guess a truth assignment and then evaluate the

circuit sat: Given a circuit, is there a truth assignment such that the circuit outputs true?.. • circuit sat ∈ NP: Guess a truth assignment and then evaluate the

circuit sat: Given a circuit, is there a truth assignment such that the circuit outputs true?.. • circuit sat ∈ NP: Guess a truth assignment and then evaluate the

• Color the nodes representing literals by their truth values (color 0 for false and color 1 for true).. – We are dealing with the a-triangles here, not the

Once you get down to a purely business level, your influence is gone and the true light of your life isdimmed. You must work in the missionary spirit, with a breadth of charity

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

Playing computer games is interesting for my brother.(To play computer games is interesting for my brother.)(It is interesting for my brother to play computer