Medium1 markMultiple Choice
Fundamentals of algorithmsGeneralarraysloopspseudo-code

AQA GCSE · Question 09.3 · Fundamentals of algorithms

What should the label L1 in Figure 8 be replaced by?

Answer options:

A.

3

B.

LEN(filmCollection)

C.

LEN(filmCollection) - 1

D.

Position

How to approach this question

1. The `FOR` loop is intended to iterate over every film in the `filmCollection` array. 2. The array is `filmCollection ← [antMan, hulk, ironMan]`. It contains 3 items. 3. With 0-based indexing, the indices of these items are 0, 1, and 2. 4. The loop structure is `FOR i ← 0 TO L1`. This means the loop will run for `i = 0, 1, ..., L1`. 5. To cover all items, the loop must run for `i = 0, 1, 2`. So, L1 must be 2. 6. Now evaluate the options. `LEN(filmCollection)` gives the number of items, which is 3. 7. Option C is `LEN(filmCollection) - 1`, which evaluates to `3 - 1 = 2`. 8. This makes the loop `FOR i ← 0 TO 2`, which is correct.

Full Answer

C.LEN(filmCollection) - 1✓ Correct
The correct answer is C (`LEN(filmCollection) - 1`). The `FOR` loop needs to iterate through each element of the `filmCollection` array. The array has 3 elements, so their indices are 0, 1, and 2. `LEN(filmCollection)` would be 3. A loop `FOR i ← 0 TO 2` is needed. Therefore, L1 should be `LEN(filmCollection) - 1`.
This question tests understanding of array indexing and loops. - The `filmCollection` array has 3 elements. - In most programming languages and in this pseudo-code (which assumes 0-based indexing), the indices of an array of length N go from 0 to N-1. - So, for `filmCollection`, the indices are 0, 1, and 2. - The `FOR` loop needs to run from the first index (0) to the last index (2). - The syntax is `FOR i ← 0 TO [last_index]`. - We need to find an expression that evaluates to 2. - `LEN(filmCollection)` returns the number of items in the array, which is 3. - Therefore, the last index is `LEN(filmCollection) - 1`, which is `3 - 1 = 2`. - Replacing L1 with this expression makes the loop correct and adaptable to changes in the array's size.

Common mistakes

✗ Choosing option B, which is a classic "off-by-one" error. A loop from 0 to `LEN(array)` will try to access an index that is out of bounds.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam