AP CSA Arrays and ArrayLists 1 — Questions and Answers
Question 1: What is the index of the first element in a Java array?
- 0 (Correct answer)
- 1
- -1
- Depends on declaration
Correct answer: 0
Java arrays are zero-indexed, so the first element is always at index 0.
Question 2: Which method adds an element to the end of an ArrayList?
- add() (Correct answer)
- append()
- insert()
- push()
Correct answer: add()
The ArrayList method add(element) appends the specified element to the end of the list.
Question 3: What is the length of an array declared as `int[] arr = new int[5];`?
- 5 (Correct answer)
- 4
- 0
- 1
Correct answer: 5
The integer passed to `new int[5]` specifies the array length as 5 elements (indices 0–4).
Question 4: Which of the following correctly accesses the last element of an array `arr` of length n?
- arr[n-1] (Correct answer)
- arr[n]
- arr[length-1]
- arr[last]
Correct answer: arr[n-1]
Since arrays are zero-indexed, the last element is at index n-1 where n is the array's length.
Question 5: What does `ArrayList<Integer> list = new ArrayList<>();` create?
- An empty ArrayList that holds Integer objects (Correct answer)
- An ArrayList pre-filled with zeros
- An ArrayList of primitive ints
- A fixed-size list of integers
Correct answer: An empty ArrayList that holds Integer objects
This statement creates an empty generic ArrayList that can store Integer wrapper objects.
Question 6: What exception is thrown when you access an array index that is out of bounds?
- ArrayIndexOutOfBoundsException (Correct answer)
- NullPointerException
- IllegalArgumentException
- IndexOutOfRangeException
Correct answer: ArrayIndexOutOfBoundsException
Java throws ArrayIndexOutOfBoundsException when you access a negative index or an index >= array length.
What is the index of the first element in a Java array?