Key Concept: Valid indices are 0 to length-1. arr[5] is out of bounds for a 5-element array (indices 0-4).
Question 4
What is printed?
int[] arr = {2, 4, 6, 8, 10};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println(sum);
Key Concept: Standard traversal pattern. Sum = 2 + 4 + 6 + 8 + 10 = 30.
Question 5
What is printed?
int[] arr = {5, 10, 15, 20};
for (int val : arr) {
System.out.print(val + " ");
}
Key Concept: Enhanced for loop (for-each) iterates through values, not indices. val takes each element's value in order.
Question 6
What is printed?
int[] arr = {3, 7, 2, 9, 4};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println(max);
Key Concept: Standard find-maximum algorithm. Initialize max to first element, then compare with remaining elements.
Question 7
What is printed?
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}
System.out.println(arr[2]);
Key Concept: The loop doubles each element. arr[2] was 3, becomes 3 * 2 = 6.
Question 8
What is printed?
int[] arr = {1, 2, 3, 4, 5};
for (int val : arr) {
val = val * 2;
}
System.out.println(arr[2]);
Key Concept: Enhanced for loops cannot modify array elements! val is a copy of the element value, not a reference to it. The array remains unchanged.
Question 9
What is printed?
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
Key Concept: Arrays are objects. int[] b = a creates an alias—both variables reference the same array. Changes through b affect a.
Question 10
What is printed?
int[] arr = {10, 20, 30, 40, 50};
int count = 0;
for (int val : arr) {
if (val > 25) {
count++;
}
}
System.out.println(count);
Key Concept: Counting elements matching a condition. Values > 25: 30, 40, 50. Count = 3.
Question 11
What is printed?
String[] names = new String[3];
System.out.println(names[0]);
Key Concept: Object arrays (including String[]) are initialized to null, not empty strings. Printing null prints "null".
Question 12
What is printed?
int[] arr = {5, 3, 8, 1, 9, 2};
int minIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
System.out.println(minIndex);
Key Concept: Finding index of minimum. The minimum value is 1 at index 3.
Question 13
What is printed?
int[] arr = {1, 2, 3, 4, 5};
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
Key Concept: Reverse traversal: start at length-1 (last index), decrement to 0. Prints elements in reverse order.
Question 14
What does this method return for arr = {2, 4, 6, 8}?
public static boolean allEven(int[] arr) {
for (int val : arr) {
if (val % 2 != 0) {
return false;
}
}
return true;
}
Key Concept: This checks if ALL elements satisfy a condition. Returns false immediately if any element is odd. All elements are even, so returns true.
ArrayList (Questions 15-28)
Question 15
Which correctly declares an ArrayList of Strings?
Key Concept: ArrayList uses angle brackets with the wrapper class name (capital S for String). Java is case-sensitive.
Question 16
What is printed?
ArrayList list = new ArrayList();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list.size());
Key Concept: size() returns the number of elements. Three elements were added, so size() returns 3.
Question 17
What is printed?
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.get(1));
Key Concept: ArrayList uses get(index) to access elements (not square brackets). Index 1 is "B".
Question 18
What is printed?
ArrayList list = new ArrayList();
list.add(5);
list.add(10);
list.add(15);
list.set(1, 99);
System.out.println(list.get(1));
Key Concept: set(index, value) replaces the element at that index. The element at index 1 changes from 10 to 99.
Question 19
What is printed?
ArrayList list = new ArrayList();
list.add("X");
list.add("Y");
list.add("Z");
list.remove(1);
System.out.println(list);
Key Concept: remove(index) removes the element and shifts subsequent elements left. Size decreases by 1. Y at index 1 is removed.
Question 20
What is printed?
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(0, 99);
System.out.println(list);
Key Concept: add(index, value) inserts at the specified position, shifting existing elements right. 99 is inserted at index 0.
Question 21
What is the problem with this code?
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println(list);
Key Concept: When removing while traversing forward, elements shift left but i increases. i=0 removes 1, list becomes [2,3]. i=1 removes 3. Result: [2]. Use backward traversal or while loop to fix.
Question 22
Which correctly removes all even numbers?
ArrayList list = new ArrayList();
// list contains [2, 5, 8, 3, 6]
Key Concept: Traverse backwards when removing! This way, index shifts don't affect unprocessed elements. Option B causes ConcurrentModificationException.
Question 23
What is printed?
ArrayList list = new ArrayList();
list.add(10);
list.add(20);
list.add(30);
for (int val : list) {
System.out.print(val + " ");
}
Key Concept: Enhanced for loops work with ArrayList. Integer auto-unboxes to int for the loop variable.
Question 24
What is the main difference between arrays and ArrayLists?
Key Concept: Arrays have fixed size. ArrayLists grow/shrink automatically. ArrayLists also only hold objects (use wrapper classes for primitives).
Question 25
What is printed?
ArrayList list = new ArrayList();
list.add("cat");
list.add("dog");
list.add("bird");
System.out.println(list.indexOf("dog"));
Key Concept: indexOf() returns the first index of the element, or -1 if not found. "dog" is at index 1.
Question 26
What is printed?
ArrayList nums = new ArrayList();
nums.add(5);
nums.add(10);
nums.add(15);
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums.get(i);
}
System.out.println(sum);
Key Concept: Standard ArrayList traversal with index. Sum = 5 + 10 + 15 = 30. Use get(i) instead of [i] for ArrayList.
Question 27
What wrapper class is used for int in ArrayList?
Key Concept: ArrayList can't hold primitives. Use wrapper classes: Integer for int, Double for double, Boolean for boolean.
Question 28
What is printed?
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
String removed = list.remove(0);
System.out.println(removed + " " + list.size());
Key Concept: remove(index) returns the removed element. "A" is removed and returned. Size goes from 2 to 1.
Key Concept: arr[0] = arr[1] makes both rows reference the same array (aliasing). Changing arr[1][0] also changes arr[0][0].
Question 37
What is printed?
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int count = 0;
for (int[] row : grid) {
for (int val : row) {
if (val % 2 == 0) {
count++;
}
}
}
System.out.println(count);
Key Concept: Counting even numbers: 2, 4, 6, 8 are even. Count = 4.
Question 38
What is printed?
String[][] words = {{"A", "B"}, {"C", "D", "E"}};
System.out.println(words[1].length);
Key Concept: Each row can have different length. Row 1 has 3 elements: "C", "D", "E".
Question 39
What is printed?
int[][] matrix = {{1, 2}, {3, 4}};
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
if (r == c) {
matrix[r][c] *= 2;
}
}
}
System.out.println(matrix[0][0] + matrix[1][1]);
Key Concept: Diagonal elements (r == c) are doubled. matrix[0][0] = 1*2 = 2, matrix[1][1] = 4*2 = 8. Sum = 10.
Question 40
What is printed?
int[][] arr = new int[2][3];
System.out.println(arr[1][2]);
Key Concept: int arrays default to 0. This is a valid index (row 1, col 2 of a 2x3 array).
Question 41
What does this code compute?
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
int result = 0;
for (int c = 0; c < grid[0].length; c++) {
result += grid[0][c];
}
System.out.println(result);
Key Concept: grid[0][c] accesses only row 0, iterating through all columns. Sum = 1 + 2 + 3 = 6.
Question 42
What does this code compute?
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
int result = 0;
for (int r = 0; r < grid.length; r++) {
result += grid[r][0];
}
System.out.println(result);
Key Concept: grid[r][0] accesses column 0, iterating through all rows. Sum = 1 + 4 = 5.
Mixed Applications & Algorithms (Questions 43-50)
Question 43
What is printed? (Linear search)
int[] arr = {5, 3, 8, 1, 9};
int target = 8;
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
System.out.println(index);
Key Concept: Linear search checks each element until found. 8 is at index 2. break exits the loop early.
Question 44
What is printed?
ArrayList list = new ArrayList();
list.add(10);
list.add(20);
list.add(30);
list.add(1, 15);
System.out.println(list.get(2));
Key Concept: After add(1, 15), list is [10, 15, 20, 30]. Element at index 2 is 20.
Key Concept: int[] copy = arr creates an alias, not a true copy. Both variables point to the same array object.
Question 46
What is printed?
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
int sum = 0;
for (int r = 0; r < arr.length; r++) {
sum += arr[r][arr[r].length - 1];
}
System.out.println(sum);
Key Concept: arr[r][arr[r].length - 1] gets the last element of each row. 2 + 4 + 6 = 12.
Question 47
What is printed?
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("B");
list.remove("B");
System.out.println(list);
Key Concept: remove(Object) removes the FIRST occurrence only. First "B" at index 1 is removed. Second "B" remains.
Key Concept: Sum = 4+2+7+1+5 = 19. Average = 19/5 = 3.8. Using double preserves decimal.
Question 49
What is printed?
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
int max = grid[0][0];
for (int[] row : grid) {
for (int val : row) {
if (val > max) {
max = val;
}
}
}
System.out.println(max);
Key Concept: Standard find-max in 2D array using enhanced for loops. Maximum value is 6.
Question 50
Which is true about arrays and ArrayLists?
Key Concept: Enhanced for loops work with both arrays and ArrayLists. Arrays use [], ArrayList uses get(). ArrayList auto-resizes, arrays don't. ArrayList needs wrapper classes for primitives.
🎉 Unit 4: Data Collections Complete!
Review your answers above or click Reset to practice again.
New 4-Unit AP CSA Curriculum (2025-26) • APCSExamPrep.com
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.