Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Finding and correcting errors in algorithms

Practise 37 exam-style questions on Finding and correcting errors in algorithms for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 37 questions
  • 1–8 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

This algorithm should output the total of five marks, but a statement is missing from the start.

FOR Index ← 1 TO 5
    INPUT Mark
    Total ← Total + Mark
NEXT Index
OUTPUT Total

State the missing statement.

Model answer

Total ← 0

Explanation

A total must be initialised before it is used. Without Total ← 0, the first time Total ← Total + Mark runs, Total has no value. The initialisation must be before the loop, not inside it, or the total would be reset each time.

Mark scheme
  1. 1 mark

    Total ← 0 (before the loop)

Question 2

2 marks

This algorithm should count how many of ten positive whole numbers input are even. It contains one error.

01Evens ← 0
02FOR Count ← 1 TO 10
03    INPUT Number
04    IF DIV(Number, 2) = 0
05      THEN
06        Evens ← Evens + 1
07    ENDIF
08NEXT Count
09OUTPUT Evens, " even numbers"

Identify the line number of the error and write the corrected line.

Model answer

Line 04: IF MOD(Number, 2) = 0

Explanation

A number is even if dividing it by 2 leaves no remainder, which is tested with MOD. DIV gives the whole-number result of the division, which is only 0 for the number 1, so the algorithm counts the wrong thing.

Mark scheme
  1. 1 mark

    Line 04

  2. 1 mark

    IF MOD(Number, 2) = 0

Question 3

3 marks

The array Names[1:50] holds 50 names. This algorithm should search for a name entered by the user. It contains one error.

01OUTPUT "Enter the name to find"
02INPUT SearchName
03Found ← TRUE
04Index ← 0
05WHILE Found = FALSE AND Index < 50 DO
06    Index ← Index + 1
07    IF Names[Index] = SearchName
08      THEN
09        Found ← TRUE
10    ENDIF
11ENDWHILE
12IF Found = TRUE
13  THEN
14    OUTPUT "Found at position ", Index
15  ELSE
16    OUTPUT "Not found"
17ENDIF

Identify the line number of the error, write the corrected line and describe the effect of the error.

Model answer

Line 03 should be: Found ← FALSE
As written, Found = FALSE is false before the loop starts, so the loop body never runs. The final IF finds Found is TRUE, so the algorithm always outputs Found at position 0, whatever name is entered.

Explanation

A flag that records whether something has happened must start as FALSE. A pre-condition (WHILE) loop is checked before its first pass, so a wrong initial value can stop it running at all. Tracing the first few lines with any name shows this at once.

Mark scheme
  1. 1 mark

    Line 03

  2. 1 mark

    Found ← FALSE

  3. 1 mark

    The WHILE condition is false at the start, so the loop never runs and the output is always Found at position 0

    Accept: no names are checked and it always reports the name as found.

Question 4

5 marks

This algorithm should output the times table for a number entered by the user, from 1 × Number up to 12 × Number, in the form 3 x 7 = 21. It contains three errors.

01OUTPUT "Enter a number"
02INPUT Number
03FOR Count ← 0 TO 11
04    Answer ← Count + Number
05    OUTPUT Count, " x ", Number, " = ", Number
06NEXT Count

Identify the line number of each error and write the corrected line. Then give the first two lines of output from the corrected algorithm when the input is 7.

Model answer

Line 03: FOR Count ← 1 TO 12
Line 04: Answer ← Count * Number
Line 05: OUTPUT Count, " x ", Number, " = ", Answer
First two lines of output for 7:
1 x 7 = 7
2 x 7 = 14

Explanation

The loop must run from 1 to 12, the answer is a product so it needs *, and the calculated Answer (not Number) must be output. Once corrected, the first pass has Count = 1, giving 1 x 7 = 7, and the second gives 2 x 7 = 14.

Mark scheme
  1. 1 mark

    Line 03: FOR Count ← 1 TO 12

    1 mark for each line number with its correct correction.

  2. 1 mark

    Line 04: Answer ← Count * Number

    Accept Answer ← Number * Count.

  3. 1 mark

    Line 05: OUTPUT Count, " x ", Number, " = ", Answer

  4. 1 mark

    1 x 7 = 7

  5. 1 mark

    2 x 7 = 14

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.