Array CS Thinking Example 4

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

Example 4

medium
Given `scores = [12, 15, 9, 20]`, what are `scores[0]` and `scores[3]`, and what happens if code tries to read `scores[4]`?

Solution

  1. 1
    Step 1: Arrays usually start at index 0, so scores[0] = 12 and scores[3] = 20.
  2. 2
    Step 2: There is no fifth element, so trying to read scores[4] would cause an out-of-bounds error or an invalid/undefined result depending on the language.

Answer

scores[0] = 12, scores[3] = 20, and scores[4] is out of bounds.
Arrays store multiple values using numbered positions. Understanding zero-based indexing and valid index ranges is essential to avoid common programming errors.

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