p2d
p2d
Line
1 check TRUE
2 n length of ST
3 for i from 1 to n do
4 if i-th character of ST ≠ (n-i+1)-th character of ST
5 check FALSE
6 return check
(a) Dry run the algorithm with the following different string values of ST and give the corresponding
return values.
[New question]
11. Some sensors are used in a smart home.
(a) The light in a room is turned on automatically when someone enters the room. What sensor should be
used? (1 mark)
(b) There is a brightness adjustment system in the room. State what sensor should be used and describe how
the sensor helps the system adjust the brightness of the room. (3 marks)
SP-DSE-ICT 2–1 1
[Modified from DSE 2015 ICT P2D Q.1(a), (b)]
12. Func(a, b) is a function with two positive integer inputs a and b, where a ≥ b. It returns the integral
part of (a÷b). For example,
The following algorithm ALG1 processes a Boolean array B with indices from 1 to n.
ALG1
Step 1: for k from 1 to n do Step 2
Step 2: B[k] True
Step 3: B[1] False
Step 4: for i from 1 to n do Steps 5 to 7
Step 5: if B[i] = True then do Steps 6 to 7
Step 6: for j from 2 to Func(n, i)
Step 7: B[i × j] False
(b) Suppose that n = 6. Dry run ALG1. Use ‘F’ and ‘T’ to denote ‘False’ and ‘True’ respectively.
(i) What are the values in B after the first pass of the loop in Step 4? (2 marks)
30
20
10 Bottom of the stack
Operation Description
Push(S, k) Pushes a box with k apples into stack S.
Pop(S) Removes a box from stack S and returns the number of apples in the box.
Empty(S) Returns TRUE if stack S has no boxes in it; otherwise, returns FALSE.
(a) (i) Initially there is an empty stack A. Write down the final content of A after executing the following
pseudocode. (2 marks)
Push(A, 10)
Push(A, 20)
TMP Pop(A)
if Empty(A) then Push(A, 30)
(ii) Initially there is an empty stack B. Write down the final content of B after executing the following
pseudocode. (2 marks)
Push(B, 10)
Push(B, 20)
Push(B, 30)
Push(B, Pop(B)+Pop(B))
30
20
15
5 Bottom of A Bottom of B
Write down the final content of A and B after executing the following pseudocode. (3 marks)
TMP 0
while not Empty(A) do
TMP TMP + Pop(A)
if TMP > 30 then
Push(B, 30)
TMP TMP - 30
Push(B, TMP)
(c) Initially there is a non-empty stack X and an empty stack Y. REV(X,Y) is a subprogram for moving
all the boxes in stack X to stack Y, where the boxes in Y are in reverse order. An example is shown
below:
40 10
30 20
20 30
10 Bottom of X Bottom of Y Bottom of X 40 Bottom of Y
40
. . . .
. . . .
. . . .
13
99
. . 40
. .
Bottom . .
. .
N boxes 12 . .
. .
3 Bottom Bottom 13 Bottom Bottom
of A of B of A of B
It is found that the apples in the bottom N boxes in A are rotten. Write the pseudocode for removing
the bottom N boxes and keeping the remaining boxes in the original order in A with the use of
REV(X,Y). (4 marks)
(e) When implementing REV, Peter uses break points for debugging. Describe how break points can help
Peter write a program. (2 marks)
END OF SECTION C
END OF PAPER
SP-DSE-ICT 2–5 5
Database (SQL commands - based on SQL-92 Standard)
1 Relationship 1
Entity Entity One-to-One Relationship
1 Relationship M
Attribute
Attribute One-to-Many Relationship
Attribute M Relationship N
Key Attribute Many-to-Many Relationship
Participation constraints:
Relationship Relationship
Relationship Use on Mandatory side
Use on Optional side