Array CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

medium
Given array `names = ['Alice', 'Bob', 'Charlie']`, what is names[1]? What happens if you access names[3]?

Solution

  1. 1
    Step 1: names[1] = 'Bob'.
  2. 2
    Step 2: names[3] would be an index out of bounds error โ€” the valid indices are 0, 1, 2.

Answer

names[1] = 'Bob'. names[3] causes an index out of bounds error.
Accessing an index beyond the array length is a common bug. Always ensure the index is between 0 and length โˆ’ 1.

About Array

An ordered collection of values stored together under a single name and accessed by their numeric index position. Arrays allow you to store, retrieve, and manipulate multiple related values efficiently using loops and index-based access.

Learn more about Array โ†’

More Array Examples