OCR GCSE Computer Science June 2024 Paper 2, Question 3(a) and (b): finding two logic errors

OCR J277/02 June 2024, Question 3(a) and (b): define a syntax error with an example, then find and correct two logic errors in an algorithm that checks whether a total is between 10 and 20. With the broken and fixed versions to run.

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

Question 3 of the OCR GCSE Computer Science Paper 2 sat on 21 May 2024 (J277/02) starts with a definition (2 marks) and then gives you an eight line algorithm with two logic errors to find and correct (4 marks).

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

Part (a): what is a syntax error?

One mark for the meaning and one for an example.

  • Meaning: an error that breaks the rules (the grammar) of the programming language. The program will not run or translate.
  • Example: a misspelt keyword such as printt, a missing bracket, or quotation marks that do not match.

Be careful with the example. print(hello) with the quotation marks left off is not accepted, because hello could be a variable. And your definition has to be clearly different from a logic error: "the program does not work" describes both.

Part (b): the question in short

The algorithm reads two numbers, adds them to make a total, and should output "success" if the total is between 10 and 20 inclusive, and "warning" if it is not.

As written, line 03 is total = num1 + num1, and line 04 is if total >= 10 then.

Find the errors

Line 03 adds the same number twice. It should add the two different inputs:

total = num1 + num2

Line 04 only checks the bottom of the range. A total of 500 is "at least 10", so it would be a success. Both ends have to be tested, joined with and:

if total >= 10 and total <= 20 then

One mark for each line number and one for each correction.

Where the marks are lost

  • if total >= 10 and <= 20. Each side of an and must be a complete comparison. total has to be written twice.
  • or for and. Every number is either at least 10 or at most 20, so the condition would always be true.
  • > 10 and < 20. "Inclusive" means 10 and 20 count. Keep the equals signs.
  • A vague description. "Fix the range check" is not a correction. Write the line.

There is a third fault that the mark scheme does not count: input gives text, so num1 + num2 would join "7" and "8" to make "78". Casting with int() is accepted as part of your correction to line 03, and it costs nothing to include.

Run it

FIXED = False is the algorithm on the paper. It runs four test pairs through it. Watch 4 and 9: the right total is 13, a success, but the broken version adds 4 and 4.

FIXED = False gets two of the four tests wrong (4 and 9, and 15 and 30). FIXED = True prints warning, success, success, warning.
The program
from bugbot import *
connect()

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

TESTS = [[2, 3], [4, 9], [10, 10], [15, 30]]
for num1, num2 in TESTS:
    if FIXED:
        total = num1 + num2
        ok = total >= 10 and total <= 20
    else:
        total = num1 + num1
        ok = total >= 10
    if ok:
        print(num1, num2, "total", total, "success")
    else:
        print(num1, num2, "total", total, "warning")
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 2024 Paper 2 Question 3(b)?

Line 03 should be total = num1 + num2. Line 04 should be if total >= 10 and total <= 20 then.

What is a syntax error?

An error that breaks the grammar rules of the programming language, such as a misspelt keyword or a missing bracket. The program cannot be translated, so it does not run.

How do I check that a number is between two values?

Use two complete comparisons joined with AND, such as total >= 10 and total <= 20. Use >= and <= if the end values are allowed, and > and < if they are not.

More from this paper

Every OCR J277 question we have worked · Guide: Logic gates and truth tables explained

Learn it step by step

  1. F6.4 Debugging logic errors Robust programs
  2. F1.4 Errors: syntax, runtime and logic Programming basics
  3. F2.3 else, elif and Boolean operators Decisions and loops
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.