Medium1 markMultiple Choice
Fundamentals of algorithmsGeneraltracealgorithmspseudo-code

AQA GCSE · Question 02.2 · Fundamentals of algorithms

In the algorithm in Figure 2, what will be output when the user input is 10?

Answer options:

A.

0

B.

1

C.

2

D.

4

How to approach this question

1. This is a trace question. Start with the user input `a = 10`. 2. The outer `WHILE` loop (line 2) starts. 3. The `IF a > 0` (line 4) is true. 4. `counter` is set to 0 (line 5). 5. The inner `WHILE a > 0` (line 6) starts. - **Pass 1:** `a` is 10. `a ← 10 DIV 3` results in `a = 3`. `counter` becomes 1. - **Pass 2:** `a` is 3. `a ← 3 DIV 3` results in `a = 1`. `counter` becomes 2. - **Pass 3:** `a` is 1. `a ← 1 DIV 3` results in `a = 0`. `counter` becomes 3. 6. The inner `WHILE` loop condition `a > 0` is now false, so it ends. 7. The algorithm proceeds to line 13, `OUTPUT a`. 8. The current value of `a` is 0. So, 0 is output.

Full Answer

A.0✓ Correct
The correct answer is A (0). The algorithm repeatedly divides the input `a` by 3 until `a` is no longer greater than 0. The final value of `a` is then output. For an input of 10, the sequence for `a` is 10 -> 3 -> 1 -> 0. The final value is 0.
A dry run or trace is needed to solve this. - **Input:** `a = 10` - The condition `a > 0` is true. - The inner `WHILE` loop begins. - **Iteration 1:** - `a` is 10. `a > 0` is true. - `a = 10 DIV 3` (integer division) which is 3. - `counter` becomes 1. - **Iteration 2:** - `a` is 3. `a > 0` is true. - `a = 3 DIV 3` which is 1. - `counter` becomes 2. - **Iteration 3:** - `a` is 1. `a > 0` is true. - `a = 1 DIV 3` which is 0. - `counter` becomes 3. - **End of loop:** - `a` is now 0. The condition `a > 0` is false. The inner loop terminates. - The code then reaches line 13: `OUTPUT a`. - The final value of `a` is 0.

Common mistakes

✗ Confusing the output variable. The code outputs `a` (line 13), not `counter`.\n✗ Making an error in the integer division (DIV) calculation.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam