These seven questions practise the four skills in algorithm reasoning and test design. Try each one on paper before opening the answer.
Set a timer with the timed original practice session builder if you want exam pace.
Questions
Question 1. Trace this and state the output.
SET x = 3
SET y = x * 2
SET x = y + 1
OUTPUT x, y
Answer
After line 1, x = 3. After line 2, y = 6. After line 3, x = 6 + 1 = 7. The output is 7 and 6.
The common slip is to use the old x in line 3.
Question 2. What does this output, and how many times does the loop body run?
SET count = 0
FOR i = 1 TO 10
IF i MOD 3 = 0 THEN
SET count = count + 1
END IF
NEXT i
OUTPUT count
Answer
The loop body runs 10 times, for i = 1 to 10. The condition is true for i = 3, 6 and 9 only, so count reaches 3. The output is 3.
Question 3. A school bus concession is for children aged 7 to 12 inclusive. List five test inputs that include both boundaries and both values just outside.
Answer
Use 9 as a normal value, 7 and 12 as boundaries, and 6 and 13 just outside. Expected results: 9, 7 and 12 accepted; 6 and 13 rejected. An invalid entry such as “eight” makes a sixth test.
Question 4. Contract: output PASS if the average of two marks is 50 or more, otherwise FAIL. Algorithm A tests (m1 + m2) / 2 >= 50, and Algorithm B tests m1 >= 50 AND m2 >= 50.
Which meets the contract? Use inputs 40 and 62.
Answer
The average of 40 and 62 is 102 ÷ 2 = 51, which is at least 50, so the contract expects PASS. Algorithm A gives PASS. Algorithm B finds 40 >= 50 false and gives FAIL. Algorithm A meets the contract; B tests a different rule.
Question 5. Classify each check as validation or verification.
- (a) A password is typed twice and the entries are compared.
- (b) A phone number must have 10 or 11 digits.
- (c) A screen shows “You entered RM2500. Is this correct?”
- (d) A name field cannot be left empty.
Answer
(a) Verification, because it checks accuracy against what the user meant. (b) Validation, a length check. (c) Verification, since the user confirms the intended amount. (d) Validation, a presence check.
Question 6. Trace the loop and state the final count.
SET n = 20
SET count = 0
WHILE n > 1
SET n = n DIV 2
SET count = count + 1
END WHILE
OUTPUT count
Answer
Here DIV gives the whole-number part of the division. n becomes 10 (count 1), then 5 (count 2), then 2 (count 3), then 1 (count 4). The check 1 > 1 is false, so the loop stops. The output is 4.
Question 7. This algorithm should output the average of five marks. The marks entered are 60, 70, 80, 90 and 100. Find the fault.
SET total = 0
FOR i = 1 TO 5
INPUT mark
SET total = total + mark
NEXT i
OUTPUT total / 4
Answer
The total is 60 + 70 + 80 + 90 + 100 = 400. The correct average is 400 ÷ 5 = 80. The algorithm outputs 400 ÷ 4 = 100, because it divides by 4 instead of 5. Change the last line to OUTPUT total / 5.
If you got these wrong
Match each question to the lesson that teaches the skill.
- Questions 1, 2 and 6: read writing a trace table that exposes an off-by-one error.
- Question 3: read choosing normal, boundary and invalid test data.
- Question 4: read comparing two algorithms against one contract.
- Question 5: read separating validation from verification.
Record each slip in the mistake log and paper-error review. If you want a teacher to go through them with you, see online one-to-one Computer Science tuition.