Mathematics I / Arrays and Lists
Practice question · Sort into groups

A list has length 5 and uses zero-based indexing. Sort each access.

Groups: Valid · Out of bounds

Hints
  1. Write out the valid index range explicitly before judging.
  2. 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.

Read the lesson: Arrays and Lists →

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