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
How do you find the number of elements in an ArrayList called `list`?
Answer: list.size()
ArrayList uses the size() method (not length) to return the number of elements it contains.
What is the result of `int[] arr = {3, 1, 4, 1, 5}; System.out.println(arr[2]);`?
Answer: 4
arr[2] accesses the third element (index 2), which is 4 in the array {3, 1, 4, 1, 5}.
Which loop is most commonly used to iterate over every element in an array in AP CSA?
Answer: for-each (enhanced for loop)
The enhanced for loop (for-each) is idiomatic for iterating all elements when the index is not needed.
What does `list.remove(0)` do to an ArrayList?
Answer: Removes the element at index 0
When passed an int, ArrayList's remove(int index) removes the element at that index, shifting remaining elements left.
Which of these correctly initializes a 2D array with 3 rows and 4 columns?
Answer: int[][] grid = new int[3][4];
The syntax `new int[rows][columns]` creates a 2D array; `new int[3][4]` gives 3 rows and 4 columns.
What is the output of: `int[] a = {10, 20, 30}; for(int x : a) System.out.print(x + " ");`
Answer: 10 20 30
The enhanced for loop assigns each element of the array to x in order, printing 10, 20, then 30.