Practice question · Match the pairs
Match each array operation to what it does.
- Access a[i]
- Length
- Traverse
- Out of bounds
- Reads the element at position i directly
- Visits every element in turn, usually with a loop
- Reports how many elements the list holds
- Uses an index outside 0 to n-1
Hints
- One of these is not an operation but an error.
- One is constant-time; one costs a pass over everything.
Show the answer
- Access a[i] → Reads the element at position i directly
- Length → Reports how many elements the list holds
- Traverse → Visits every element in turn, usually with a loop
- Out of bounds → Uses an index outside 0 to n-1
Why
Indexed access is direct and cheap; traversal costs a full pass. Length and the last valid index differ by one, and mixing them up is precisely what produces the out-of-bounds error.
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…
- A list has length 5 and uses zero-based indexing. Sort each access.
- 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.
- 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…