Skip to content
SPM Tuition
Program structure and debugging practice

Program structure and debugging practice

You understand the debugging lessons and want faulty programs you have not met before.

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 is with the closing quotation mark missing.
  • (b) Using AND where the rule needs OR, 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

Log your slips with the mistake log and paper-error review. For teacher support, see online one-to-one Computer Science tuition.

Common questions

How should I approach a faulty program?

Trace it on a small input where you know the correct answer, find the first row that differs from your expectation, and then correct that line only. Re-trace to confirm.

Do I need to write the full corrected program?

Usually only the faulty line or lines, plus a short explanation. Rewriting the whole program wastes time and hides whether you found the exact fault.

Are these questions from past papers?

No. They are original questions written for this site, with every number checked by hand, and they do not reproduce any examination paper.

If explaining the fix was harder than finding it, a one-to-one Computer Science teacher can practise the wording with you on new faulty programs.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.