Arrays and ArrayLists Flashcards
6 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Arrays and ArrayLists flashcards as text
What is the index of the first element in a Java array?
Answer: 0
Java arrays are zero-indexed, so the first element is always at index 0.
Which method adds an element to the end of an ArrayList?
Answer: add()
The ArrayList method add(element) appends the specified element to the end of the list.
What is the length of an array declared as `int[] arr = new int[5];`?
Answer: 5
The integer passed to `new int[5]` specifies the array length as 5 elements (indices 0โ4).
Which of the following correctly accesses the last element of an array `arr` of length n?
Answer: arr[n-1]
Since arrays are zero-indexed, the last element is at index n-1 where n is the array's length.
What does `ArrayList list = new ArrayList<>();` create?
Answer: An empty ArrayList that holds Integer objects
This statement creates an empty generic ArrayList that can store Integer wrapper objects.
What exception is thrown when you access an array index that is out of bounds?
Answer: ArrayIndexOutOfBoundsException
Java throws ArrayIndexOutOfBoundsException when you access a negative index or an index >= array length.