These seven questions practise the skills in program structure and debugging. Try each on paper before opening the answer.
For exam pace, use the timed original practice session builder.
Questions
Question 1. Trace and state the output. Then say whether the last line would work.
GLOBAL count = 0
PROCEDURE step(k)
LOCAL extra = 2
SET count = count + k + extra
END PROCEDURE
step(1)
step(4)
OUTPUT count
OUTPUT extra
Answer
After step(1), count = 0 + 1 + 2 = 3. After step(4), count = 3 + 4 + 2 = 9. The first OUTPUT gives 9. The second fails, because extra is local and no longer exists in the main program.
Question 2. Why does this loop never end?
SET n = 20
WHILE n <> 0
SET n = n - 6
END WHILE
Answer
The values are 20, 14, 8, 2 and then −4, −10 and so on. Zero is never reached, because the step of 6 jumps over it. Change the condition to n > 0.
Question 3. Five scores are entered: 40, 65, 0, 72, 58. The program below reads until a score of 0. What total does it give, and what should it give?
SET total = 0
INPUT s
WHILE s <> 0
SET total = total + s
INPUT s
END WHILE
OUTPUT total
Answer
It adds 40 and 65, then stops at 0, so the total is 105. The correct total is 40 + 65 + 0 + 72 + 58 = 235. A real score of 0 is ending the loop. Use FOR i = 1 TO 5 instead.
Question 4. Classify each fault.
- (a)
OUTPUT "Total iswith the closing quotation mark missing. - (b) Using
ANDwhere the rule needsOR, so a valid input is rejected. - (c) Dividing by a variable that holds 0 at that moment.
Answer
(a) Syntax error, a broken language rule. (b) Logic error, since the program runs and gives a wrong decision. (c) Runtime error, because the program stops while running.
Question 5. This program should output the highest of three marks but outputs the lowest. Find the smallest correction.
SET best = m1
IF m2 < best THEN
SET best = m2
END IF
IF m3 < best THEN
SET best = m3
END IF
OUTPUT best
Answer
Both comparisons use <, so the program keeps the smallest value. Change both to >. For 50, 90 and 70, the corrected program gives best = 50, then 90, then stays 90, so it outputs 90.
Question 6. The average of four marks 40, 50, 60 and 70 should be 55. Find why the program outputs 17.5.
FOR i = 1 TO 4
SET total = 0
INPUT mark
SET total = total + mark
NEXT i
OUTPUT total / 4
Answer
SET total = 0 is inside the loop, so total resets on every pass. At the end, total holds only the last mark, 70, and the output is 70 ÷ 4 = 17.5. Move the line above the loop. Then the total is 220 and the output is 220 ÷ 4 = 55.
Question 7. Explain, using the four-part frame, how to correct this program so it prints the multiples of 4 from 4 to 20.
FOR i = 1 TO 5
OUTPUT i + 4
NEXT i
Answer
Where: the OUTPUT line. What: it adds 4 to i. Why: it prints 5, 6, 7, 8, 9.
Correction: write OUTPUT i * 4. Check: i = 1 to 5 gives 4, 8, 12, 16, 20, which are the required multiples.
If you got these wrong
- Question 1: read tracing variable scope across a procedure and its caller.
- Questions 2 and 3: read diagnosing a loop that terminates too early or never terminates.
- Questions 4 and 5: read distinguishing a syntax error from a logically wrong result.
- Questions 6 and 7: read explaining a small correction.
Log your slips with the mistake log and paper-error review. For teacher support, see online one-to-one Computer Science tuition.