OCR GCSE Computer Science June 2023 Paper 2, Question 2: two logic errors in an array total

OCR J277/02 June 2023, Question 2: define syntax error and logic error, then find and correct two logic errors in a loop that should total an array. Both errors explained, with the broken and fixed algorithm to run.

Past paper questionOCR J277/02June 2023 Paper 26 marksFix the code

Question 2 of the OCR GCSE Computer Science Paper 2 sat on 25 May 2023 (J277/02) shows a five line pseudocode algorithm that should add up the numbers in an array, and does not. Part (a) asks what syntax and logic errors are (2 marks). Part (b) asks you to find two logic errors and write the corrected lines (4 marks).

We do not copy the exam paper here. Open it beside this page: OCR June 2023 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

Part (a): the two definitions

  • Syntax error: an error in the rules or grammar of the programming language. The program will not run (or will not translate).
  • Logic error: the program runs, but produces an incorrect or unexpected result.

The question asks what each term means, so an example on its own is not enough. "An error in the syntax" and "an error in the logic" say nothing and get nothing.

Part (b): the question in short

scores is a 0-indexed array. The algorithm sets total to 0, loops scoreCount from 1 to the last index, and on each pass does scores[scoreCount] = total + total. Then it prints total.

Find the errors

Line 02 starts at 1. The array is 0-indexed, so the first score is at position 0. Starting at 1 misses it.

for scoreCount = 0 to scores.length - 1

Line 03 is back to front. It stores something in the array and never changes total. Worse, what it stores is total + total, which is 0 + 0. The running total pattern is: new total = old total + this item.

total = total + scores[scoreCount]

One mark for each line number and one for each correction. The correction only counts if it goes with the right line number.

Where the marks are lost

  • "Fixing" the end of the loop. scores.length - 1 is right: five items have the indexes 0 to 4. The mark scheme does not penalise you for removing the - 1, but it is not one of the errors.
  • A correction with the wrong line number. It is not marked at all.
  • Calling them syntax errors. Every line here is valid pseudocode. The algorithm would run. It would print 0 and wreck the array.
  • Definitions that only say "it does not work". That is true of both kinds of error, so it defines neither.

Run it

FIXED = False is the algorithm on the paper, translated to Python. It runs without complaint, prints a total of 0, and overwrites the scores. That is what makes a logic error dangerous. Change FIXED to True for the corrected version. The robot drives the total in centimetres.

FIXED = False prints a total of 0 and ruins the array. FIXED = True prints 40 and leaves the scores alone.
The program
from bugbot import *
connect()

# False is the algorithm on the paper. True is the corrected one
FIXED = False

scores = [12, 7, 9, 4, 8]
total = 0
if FIXED:
    for scoreCount in range(0, len(scores)):
        total = total + scores[scoreCount]
else:
    for scoreCount in range(1, len(scores)):
        scores[scoreCount] = total + total
print("total is", total)
print("scores is now", scores)
forward(60, distance=total)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2023 Paper 2 Question 2(b)?

Line 02 should start the loop at 0, so that the first element is included. Line 03 should be total = total + scores[scoreCount], so that each score is added to the running total.

What is the difference between a syntax error and a logic error?

A syntax error breaks the grammar rules of the language, and the program will not run until it is fixed. A logic error does not stop the program running, but makes it give the wrong result.

How do you total the values in an array?

Set a total to 0 before the loop. Loop over every index from 0 to the length minus 1, adding each element to the total. Output the total after the loop.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F6.4 Debugging logic errors Robust programs
  2. F3.4 Iterating over a list Strings, lists and records
  3. F1.4 Errors: syntax, runtime and logic Programming basics
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.