Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Identifying and correcting errors in algorithms

Practise 29 exam-style questions on Identifying and correcting errors in algorithms for OCR GCSE 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 the Study Helper on hand if you get stuck.

  • 29 questions
  • 1–6 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

2 marks

This algorithm should print exactly 5 stars.

01stars = 0
02while stars <= 5
03    print("*")
04    stars = stars + 1
05endwhile

State how many stars the algorithm actually prints, and write a corrected line 02.

Model answer

It prints 6 stars.

while stars < 5

Explanation

The loop runs for stars = 0, 1, 2, 3, 4 and 5, which is six passes, because <= includes 5. With stars < 5 it runs for 0 to 4, which is exactly five passes. This off-by-one error is a very common logic error: always check the first and last values that a loop condition allows.

Mark scheme
  1. 1 mark

    6

  2. 1 mark

    while stars < 5

    accept while stars <= 4 / while stars != 5; accept Python. Also accept changing line 01 to stars = 1 if the student clearly says line 02 stays the same, as this also prints 5 stars.

Question 2

3 marks

Priya's program should reject any test mark that is less than 0 or greater than 100.

01mark = int(input("Enter mark (0 to 100)"))
02if mark < 0 AND mark > 100 then
03    print("Invalid mark")
04else
05    print("Mark accepted")
06endif

When Priya enters 150, the program outputs Mark accepted.

Identify the line that contains the error, explain why the error happens, and write the corrected line of code.

Model answer

The error is on line 02. A mark cannot be both less than 0 and greater than 100 at the same time, so the AND condition is never true and every mark, including 150, goes to the else branch. The condition needs OR:

if mark < 0 OR mark > 100 then

Explanation

AND is only true when both parts are true, and no number is below 0 and above 100 at once, so the check never rejects anything. OR is true when either part is true, which is what a range check outside 0 to 100 needs. This is a logic error: the program runs, but invalid data such as 150 or -5 is accepted. Testing with invalid data revealed it.

Mark scheme
  1. 1 mark

    line 02

    accept 2 or 02

  2. 1 mark

    a mark cannot be less than 0 and greater than 100 at the same time // the condition can never be true, so the else branch always runs

    'AND should be OR' on its own is not an explanation: it is credited by the third mark point when the corrected line is written

  3. 1 mark

    if mark < 0 OR mark > 100 then

    accept equivalent conditions, e.g. if mark <= -1 OR mark >= 101 then / if NOT(mark >= 0 AND mark <= 100) then; accept Python (or); ignore minor syntax errors

Question 3

4 marks

Programmers need to find and fix both syntax errors and logic errors.

Explain the difference between a syntax error and a logic error. Give an example of each in your answer.

Model answer

A syntax error is code that breaks the rules (grammar) of the programming language, so the program cannot be translated or run. For example, print("Hello" has a missing closing bracket. A logic error is when the program runs but does not do what the programmer intended, so it produces an unexpected output. For example, writing area = length + width when the area should be length * width.

Explanation

The key difference is whether the program runs: a syntax error stops the code being translated, while a program with a logic error runs but gives the wrong result. Syntax errors include misspelt keywords, missing brackets and missing words such as then or endif. Logic errors include wrong operators, wrong comparisons and loops that run the wrong number of times. Make sure each example matches its error type.

Mark scheme
  1. 1 mark

    syntax error: the code breaks the rules/grammar of the programming language // the code cannot be translated/run

    accept 'the program will not compile/run'; do not accept 'a spelling mistake' on its own as the definition (it can be credited as an example)

  2. 1 mark

    a valid example of a syntax error, e.g. a misspelt keyword (prnt, whle) / a missing bracket or quote mark / a missing then, endif, endwhile or next / using a keyword as a variable name

    accept a line of code that contains a syntax error, e.g. print("Hello" ; the example must be a syntax error, not a logic error

  3. 1 mark

    logic error: the program runs but produces an unexpected/incorrect output // the program does not do what the programmer intended

    do not accept 'the program crashes' or 'the program will not run'

  4. 1 mark

    a valid example of a logic error, e.g. using + instead of * / using > instead of >= / a loop that repeats the wrong number of times / an infinite loop / resetting a total inside a loop / using the wrong variable

    accept a line of code with its intended purpose, e.g. 'area = length + width when the area should be length times width'; the example must be a logic error

Question 4

5 marks

This program should output the mean of the numbers entered by the user. The user enters -1 to stop.

01total = 0
02count = 0
03num = 0
04while num != -1
05    num = int(input("Enter a number, -1 to stop"))
06    total = total + num
07    count = count + 1
08endwhile
09print("Mean: " + str(total / count))

The user enters 4, 6, 8 and then -1.

State the output of the program and explain the logic error. Rewrite the program so that it outputs the correct mean, using OCR Exam Reference Language or a high-level programming language. State the output of your program for the same inputs.

Model answer

The program outputs Mean: 4.25. The -1 is entered to stop, but it is still added to the total and counted, so the program calculates 17 / 4 instead of 18 / 3.

total = 0
count = 0
num = int(input("Enter a number, -1 to stop"))
while num != -1
    total = total + num
    count = count + 1
    num = int(input("Enter a number, -1 to stop"))
endwhile
print("Mean: " + str(total / count))

For the inputs 4, 6, 8 and -1, this program outputs Mean: 6.

Explanation

With a stopping (rogue) value, the value must be checked before it is processed. Reading the first number before the loop, and the next number at the end of each pass, means the while condition checks every number before it is added or counted. The original program adds 4 + 6 + 8 + (-1) = 17 and counts 4 numbers, giving 4.25 instead of 18 / 3 = 6. A fuller solution would also check that count is not 0 before dividing.

Mark scheme
  1. 1 mark

    Mean: 4.25

    accept 4.25. For the rewritten program: answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors.

  2. 1 mark

    the -1 (stopping value) is added to the total and counted // the total becomes 17 and the count 4, instead of 18 and 3

  3. 1 mark

    rewritten program: the -1 is not added to the total

    e.g. the first input is before the loop and the next input is at the end of the loop body, or the addition is inside if num != -1 then

  4. 1 mark

    rewritten program: the -1 is not counted

    the program must still stop when -1 is entered and output total / count; do not award if the loop no longer stops at -1

  5. 1 mark

    Mean: 6

    accept 6 or 6.0; award only if the rewritten program would really output 6 for these inputs. A missing int() cast on the input is a minor slip and is not penalised at this or any other mark point.

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

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.