191k views
3 votes
If our list called List is populated as [0, 1, 2, 3, 4], what happens if our code tries to access List [5] per the rules of the AP Computer Science Principles Reference Guide?

1 Answer

4 votes

Answer:

The number 4 will be returned.

Step-by-step explanation:

Given;

List = [0, 1, 2, 3, 4]

Using the AP Reference Guide;

The first element has an index of 1 and can be accessed using the same. i.e

=> List[1] = 0

The second element has an index of 2 and can be accessed using the same. i.e

=> List[2] = 1

Therefore, if our code tries to access List[5], then the number at index 5 will be returned. i.e

=> List[5] = 4

This implies that the last number 4 will be returned.

Note: Many programming languages (such as Java) actually start their collection (array, list) index from 0 rather than 1. But since we are using the AP Computer Science Principles Reference Guide, the index has to start from 1.

User Gmauch
by
7.6k points