AQA GCSE Computer Science June 2023 Paper 1, Question 4: tracing a Python function

AQA 8525 June 2023 Paper 1, Question 4: trace a Python function that works out an area or a volume for three sets of inputs, then say how to make the program more robust. Worked through, with a program you can run.

Past paper questionAQA 8525/1BJune 2023 Paper 14 marksTrace table

Question 4 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) gives you a short Python program with a function in it. Part 1 asks you to complete a trace table for three sets of inputs (3 marks). Part 2 asks for one way to make the program more robust (1 mark).

We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The question in short

The program asks for three whole numbers: a width, a length and a height. The user types -1 for the height to mean "ignore it".

A function takes all three. If the height is -1 it returns width times length. Otherwise it returns width times length times height.

Back in the main program, if the height typed was -1 it prints the word Area and the answer. Otherwise it prints the word Volume and the answer.

The trace table gives you three rows of inputs and one empty column: the final output.

width length height
5 6 -1
10 4 0
3 5 10

Work it through

Row 1. The height is -1, so the function returns 5 x 6 = 30. The height is -1 in the main program too, so the output is Area 30.

Row 2. This is the trap. The height is 0, and 0 is not -1. So the function multiplies all three: 10 x 4 x 0 = 0. The main program takes the else branch as well. The output is Volume 0.

Row 3. The height is 10. The function returns 3 x 5 x 10 = 150. The output is Volume 150.

The finished trace table

width length height Final output
5 6 -1 Area 30
10 4 0 Volume 0
3 5 10 Volume 150

Where the marks are lost

  • Writing only the number. The program prints a word and a number. 30 on its own is not what it outputs. The mark scheme caps an answer with numbers only (or words only) at 2 marks, and at 1 mark if there is another error as well.
  • Treating 0 like -1. A box with no height feels like an area. The program does not agree. It tests for exactly -1, so 0 is a volume, and the volume is 0.
  • Adding extra characters. Brackets, a colon or a pound sign that the program would not print can cost a mark. Quotation marks round the output are ignored.

Part 2: make it more robust

A robust program copes with input it did not expect. One mark, for one of these:

  • Validate the inputs. Check that the width and length are positive, and that the height is either -1 or positive. Ask again if not.
  • Accept decimals. The program converts every input with int, so typing 2.5 crashes it. Converting with float fixes that.

Say what you would check. "Add validation" is enough for the mark here, and an example makes it safe.

Run it

Here is a program that does the same job. Instead of asking you to type, it runs the three rows of the trace table one after another. Add a row of your own to TESTS and predict the output before you press Run.

It prints Area 30, then Volume 0, then Volume 150: the last column of the trace table.
The program
from bugbot import *
connect()

# each row is width, length, height. Add your own
TESTS = [[5, 6, -1], [10, 4, 0], [3, 5, 10]]

def calculate(width, length, height):
    if height == -1:
        return width * length
    else:
        return width * length * height

for width, length, height in TESTS:
    answer = calculate(width, length, height)
    if height == -1:
        print("Area", answer)
    else:
        print("Volume", answer)
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.

Now change it

Add the row [4, 2.5, -1]. It works here because the numbers are already in the list. In the exam program it would crash, because int("2.5") is an error. That is the second answer to Part 2.

Questions

What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 4.1?

The three outputs are Area 30, Volume 0 and Volume 150. The middle row catches people out: a height of 0 is not -1, so the program multiplies all three numbers and prints Volume 0.

What does robust mean in GCSE Computer Science?

A robust program keeps working when it is given unexpected or wrong input. You make a program more robust with validation, such as range checks and type checks, and by handling errors so that it does not crash.

Do I need to write the word as well as the number in a trace table output column?

Yes. The output column must show exactly what the program prints. If it prints Area 30, write Area 30. The number alone loses a mark on this question.

More from this paper

Every AQA 8525 question we have worked · Guide: Trace tables explained

Learn it step by step

  1. F13.2 Trace tables Exam preparation
  2. F4.2 Parameters and return values Functions and structured code
  3. F6.1 Defensive design and validation Robust programs
Open the lessons

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.