IGCSE · Practice questions · Finding and correcting errors in algorithms

Find it, fix it.

Six original Cambridge-style questions on identifying and correcting errors in algorithms.

Original questions All questions on this page are original work, written in the Cambridge IGCSE style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Purpose, dry run, fix.

01
Analysis
[4 marks]

This algorithm should input 5 prices and output the total and the number of prices over 20. Identify the two errors and write the corrected lines.

01 Total ← 0
02 Over ← 0
03 FOR Count ← 1 TO 5
04     INPUT Price
05     Total ← Price
06     IF Price > 20
07       THEN
08         Over ← Over + Price
09     ENDIF
10 NEXT Count
11 OUTPUT Total, " ", Over
  • Line 05: the total is replaced, not added to. ✓
  • Correct: Total ← Total + Price ✓
  • Line 08: adds the price instead of counting. ✓
  • Correct: Over ← Over + 1 ✓
02
Analysis
[2 marks]

This algorithm should output the numbers 1 to 10. Identify the error and correct it.

Number ← 1
WHILE Number <= 10 DO
    OUTPUT Number
ENDWHILE
  • Number is never changed inside the loop, so it outputs 1 for ever (infinite loop). ✓
  • Add Number ← Number + 1 inside the loop, after OUTPUT Number (before ENDWHILE). ✓
03
Analysis
[3 marks]

This algorithm should find the largest of 8 temperatures, which can be from -30 to 40. Explain the error and give two different ways to correct it.

Largest ← 50
FOR Day ← 1 TO 8
    INPUT Temp
    IF Temp > Largest
      THEN
        Largest ← Temp
    ENDIF
NEXT Day
OUTPUT Largest
  • Largest starts at 50, higher than any possible temperature, so it is never replaced and 50 is always output. ✓
  • Correction 1: Largest ← -30 (or any value lower than -30, e.g. -999). ✓
  • Correction 2: input the first temperature and set Largest to it before the loop (loop over the other 7). ✓
04
Analysis
[2 marks]

This loop should run 12 times: FOR Month ← 1 TO 11. State the problem and correct the line.

  • It runs only 11 times, so one month is missed. ✓
  • FOR Month ← 1 TO 12 ✓
05
Analysis
[3 marks]

This should swap the values of A and B. A = 4 and B = 9 at the start. State the values after the code runs, explain the error and correct it.

A ← B
B ← A
  • A = 9 and B = 9 after the code runs. ✓
  • The original value of A is overwritten before it is copied to B. ✓
  • Correct: Temp ← A; A ← B; B ← Temp. ✓
06
Analysis
[2 marks]

This validation should reject marks outside 0 to 100: UNTIL Mark >= 0 OR Mark <= 100. Explain why it accepts every number, and correct it.

  • Every number is either at least 0 or at most 100, so the OR condition is always true and the loop always ends. ✓
  • Correct: UNTIL Mark >= 0 AND Mark <= 100 ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.