Medium3 marksStructured
ProgrammingGeneraltracepythonfunctions

AQA GCSE · Question 04.1 · Programming

numOne numTwo numThree Final output 5 6 -1 10 4 0 3 5 10

Figure 3 shows a program written in Python that calculates the area of a rectangle or the volume of a box from the user inputs.

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

numOne = int(input("Enter width: "))
numTwo = int(input("Enter length: "))
numThree = int(input("Enter height, -1 to ignore: "))

answer = calculate(numOne, numTwo, numThree)

if numThree == -1:
print(f"Area {answer}")
else:
print(f"Volume {answer}")
Complete the trace table using the program in Figure 3.

How to approach this question

For each row of the table, trace the execution of the Python code with the given inputs for `numOne`, `numTwo`, and `numThree`. 1. **Row 1:** `numOne=5`, `numTwo=6`, `numThree=-1`. * The `calculate` function is called with `(5, 6, -1)`. * Inside `calculate`, `height == -1` is true, so it returns `width * length` (5 * 6 = 30). * `answer` becomes 30. * The `if numThree == -1` condition is true, so it prints "Area 30". 2. **Row 2:** `numOne=10`, `numTwo=4`, `numThree=0`. * The `calculate` function is called with `(10, 4, 0)`. * Inside `calculate`, `height == -1` is false, so it returns `width * length * height` (10 * 4 * 0 = 0). * `answer` becomes 0. * The `if numThree == -1` condition is false, so it prints "Volume 0". 3. **Row 3:** `numOne=3`, `numTwo=5`, `numThree=10`. * The `calculate` function is called with `(3, 5, 10)`. * Inside `calculate`, `height == -1` is false, so it returns `width * length * height` (3 * 5 * 10 = 150). * `answer` becomes 150. * The `if numThree == -1` condition is false, so it prints "Volume 150".

Full Answer

The completed table is: - Row 1: Final output = `Area 30` - Row 2: Final output = `Volume 0` - Row 3: Final output = `Volume 150`
This question requires a dry run (trace) of the given Python code for three different sets of inputs. **Trace for Row 1:** - Inputs: `numOne = 5`, `numTwo = 6`, `numThree = -1` - `calculate(5, 6, -1)` is called. - Inside the function, `height` is -1. The condition `if height == -1` is true. - The function returns `width * length`, which is `5 * 6 = 30`. - `answer` is assigned the value 30. - The condition `if numThree == -1` is true. - The program prints `f"Area {answer}"`, which is "Area 30". **Trace for Row 2:** - Inputs: `numOne = 10`, `numTwo = 4`, `numThree = 0` - `calculate(10, 4, 0)` is called. - Inside the function, `height` is 0. The condition `if height == -1` is false. - The `else` block executes, returning `width * length * height`, which is `10 * 4 * 0 = 0`. - `answer` is assigned the value 0. - The condition `if numThree == -1` is false. - The `else` block executes, printing `f"Volume {answer}"`, which is "Volume 0". **Trace for Row 3:** - Inputs: `numOne = 3`, `numTwo = 5`, `numThree = 10` - `calculate(3, 5, 10)` is called. - Inside the function, `height` is 10. The condition `if height == -1` is false. - The `else` block executes, returning `width * length * height`, which is `3 * 5 * 10 = 150`. - `answer` is assigned the value 150. - The condition `if numThree == -1` is false. - The `else` block executes, printing `f"Volume {answer}"`, which is "Volume 150".

Common mistakes

✗ Forgetting to include "Area " or "Volume " in the final output.\n✗ Calculating the volume when the height is -1, or vice-versa.\n✗ Making a simple multiplication error.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam