Mathematics I / Arrays and Lists
Practice question · Numerical answer

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 reads are out of bounds?

Hints
  1. Count the accesses the loop makes, then count how many indices are legal.
  2. Valid indices for seven values are 0 through 6.
Show the answer

1

Why

The loop makes 8 accesses (i = 0..7) but only 0..6 are valid, so exactly one read, b[7], is out of bounds. The loop looks nearly right, which is why off-by-one errors survive casual reading.

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