Medium1 markMultiple Choice
Fundamentals of algorithmsGeneraldebuggingalgorithmsnested loops

AQA GCSE · Question 10.2 · Fundamentals of algorithms

How could the error in the algorithm in Figure 9 be corrected?

Answer options:

A.

Change line number 3 to: count ← -1

B.

Change line number 4 to: FOR i ← 1 TO 4

C.

Change line number 7 to: FOR j ← 0 TO 2

D.

Change line number 9 to: result ← scores[j * 3 + i]

How to approach this question

1. Identify the error from the trace in the previous question or by analysing the code. The problem is that the inner loop only runs twice, but there are three scores per student. 2. The inner loop is on line 7: `FOR j ← 0 TO 1`. This iterates for `j=0` and `j=1`. 3. To make it run three times, it needs to iterate for `j=0`, `j=1`, and `j=2`. 4. The correct pseudo-code for this is `FOR j ← 0 TO 2`. 5. This matches option C.

Full Answer

C.Change line number 7 to: FOR j ← 0 TO 2✓ Correct
The correct answer is C. The error is that only two scores are displayed per student. The inner loop `FOR j ← 0 TO 1` runs for j=0 and j=1. To display three scores, it needs to run for j=0, 1, and 2. This is achieved by changing the loop to `FOR j ← 0 TO 2`.
The algorithm uses a nested loop structure to process a list of students and their corresponding scores. The outer loop (controlled by `i`) iterates through the students. The inner loop (controlled by `j`) is intended to iterate through the scores for the current student. The problem states there are three test scores for each student. The error lies in line 7: `FOR j ← 0 TO 1`. This loop only executes for `j=0` and `j=1`. It does not execute for `j=2`, so the third score for each student is never accessed or displayed. To fix this, the loop needs to include `j=2`. The correct syntax is `FOR j ← 0 TO 2`. This will make the loop run three times, correctly processing all three scores for each student.

Common mistakes

✗ Choosing an option that "fixes" a part of the code that is not broken.\n✗ Misunderstanding how `FOR` loops work in pseudo-code.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam