5 questions to test your understanding
A programmer writes a loop that runs while i <= arr.length and accesses arr[i] on each iteration. The array has 5 elements. What error will occur?
In a zero-based indexed array nums = [10, 20, 30, 40, 50], what does nums[nums.length - 1] return?
In a zero-based indexed array of length 8, the valid indices are 0 through 7.
In most programming languages, array indices start at 1, so an array of 5 elements has valid indices 1 through 5.
Explain why the last valid index of an array is length − 1 rather than length. Use a concrete example.