AQA GCSE Computer Science June 2022 Paper 1, Question 5: a syntax error and a logic error
AQA 8525 June 2022 Paper 1, Question 5: a Python program that should print ten random numbers from a list has a syntax error and a logic error. Both found and fixed, with the broken and fixed versions to run.
Question 5 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) shows a seven line Python program with two faults in it. Part 1 asks which statements about syntax errors are true (2 marks). Part 2 asks you to find and correct the logic error (2 marks). Part 3 asks what kind of data structure the program uses (1 mark).
We do not copy the exam paper here. Open it beside this page: AQA June 2022 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The program should output 10 numbers, each chosen at random from a list of eight numbers.
It imports random, makes the list, and sets count to 0. Then, while count is less than 10, it adds 1 to count, makes a random number from 0 to 7 with random.randrange(0, 8, and prints numbers[count].
Two things are wrong. The randrange line has no closing bracket. And the print line uses the wrong variable.
Part 1: what is true about syntax errors?
Two of the five statements are true:
- A syntax error is a mistake in the grammar of the code. A missing bracket, a missing colon, a misspelt keyword.
- A syntax error will stop a program from running. Python refuses to start until it is fixed.
The other three are false. Testing boundary values finds logic errors. Syntax errors are easier to spot than logic errors, because the computer points at them. And asking for the fifth character of a four character string is a run-time error: the grammar is fine, and it only fails when that line runs.
Part 2: the logic error
Line 7. It prints numbers[count], but count is just the loop counter. The random position is in number. The corrected line is:
print(numbers[number])
One mark for the line number, one for the correction.
What would the broken version do? It would print the list in order from position 1, the same every time, and then crash when count reached 8, because the list only has positions 0 to 7.
Part 3: the data structure
numbers is an array. In Python it is called a list. Either word gets the mark.
Where the marks are lost
- Giving line 6 for Part 2. Line 6 has the missing bracket, and that is the syntax error. The question asks for the logic error.
- Ticking "harder to spot". Logic errors are the hard ones. The program runs and quietly gives the wrong answer.
- Shading three boxes in Part 1. More than two and you get nothing.
- "Variable" for Part 3. It is a variable, but the question asks what type of data structure.
Run it
FIXED = False runs the program with the logic error (the bracket has been put back so that it starts at all). Watch what it prints, and where it stops. Then change it to True.
The program
from bugbot import *
import random
connect()
# False is the program on the paper. True is the corrected one
FIXED = False
numbers = [11, 14, 56, 4, 12, 6, 42, 2]
count = 0
while count < 10:
count = count + 1
number = random.randrange(0, 8)
if FIXED:
print(numbers[number])
else:
print(numbers[count]) # line 7, the logic error
Questions
What are the answers to AQA GCSE Computer Science 2022 Paper 1 Question 5?
5.1: B and D. A syntax error is a mistake in the grammar of the code, and it stops the program from running. 5.2: line 7, corrected to print(numbers[number]). 5.3: an array, or list.
What is the difference between a syntax error and a logic error?
A syntax error breaks the rules of the language, so the program will not run. A logic error is a mistake in what the program does: it runs, but gives the wrong result. Logic errors are harder to find because nothing points them out.
What does random.randrange(0, 8) do?
It gives a random whole number from 0 to 7. The first number is included and the second is not, which makes it a good fit for the positions of an eight item list.
More from this paper
Every AQA 8525 question we have worked
Learn it step by step
- F1.4 Errors: syntax, runtime and logic Programming basics
- F6.4 Debugging logic errors Robust programs
- F3.7 Random numbers Strings, lists and records
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.