Practice question · Sort into groups
A list has length 5 and uses zero-based indexing. Sort each access.
Groups: Valid · Out of bounds
- a[0]
- a[4]
- a[6]
- a[3]
- a[5]
Hints
- Write out the valid index range explicitly before judging.
- The index equal to the length is already one too far.
Show the answer
Valid: a[0], a[3], a[4]
Out of bounds: a[5], a[6]
Why
Valid indices are 0, 1, 2, 3, 4. a[5] uses the length as an index, which is exactly one past the end, and a[6] is further still. The boundary case a[5] is the one that slips through in real code.
Practise Arrays and Lists
The app has 5 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Arrays and Lists
- Arrays are indexed from 0, so a list of length 5 has valid indices 0 to 4 and a[5] is an error. Why has this…
- Appending to a dynamic array is described as O(1) amortised, even though some appends copy the entire array.…
- Let a be the list 3, 1, 4, 1, 5 with zero-based indexing. Select every TRUE statement.
- Match each array operation to what it does.
- A list b holds seven values. A loop runs i from 0 to 7 inclusive and reads b[i] each time. How many of those…