Unit 4 — Data Collections
Unit 4 is about storing and processing many values: arrays, ArrayLists, and 2D arrays. You’ll practice traversal patterns, common algorithms (searching, counting, min/max, swapping), and reasoning about indexes.
Topic pages
Go in order (4.1 → 4.15).
Array Creation and Access4.1
Traversing Arrays4.2
Enhanced for Loop for Arrays4.3
Developing Algorithms Using Arrays4.4
ArrayList Creation and Access4.5
Traversing ArrayLists4.6
Enhanced for Loop for ArrayLists4.7
Developing Algorithms Using ArrayLists4.8
ArrayList Methods4.9
ArrayList and Arrays4.10
Exploring 2D Arrays4.11
2D Array Creation and Access4.12
Traversing 2D Arrays4.13
Enhanced for Loop for 2D Arrays4.14
Developing Algorithms Using 2D Arrays4.15
How to use the quiz
- Do MCQ first, then check answers at the end.
- Pay attention to index bounds and loop conditions.
- FRQ is focused on a classic traversal + algorithm pattern.
Unit 4 Quiz — Multiple Choice (15)
Choose the best answer. (One correct option each.)
1) Which statement correctly creates an int array of length 5?
Answer: B. Arrays use
[] and new int[length].2) What is the valid index range for an array with length n?
Answer: D. Java arrays are zero-indexed: last index is
length - 1.3) What is printed?
int[] a = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println(sum);
Answer: A. Sum is 2+4+6+8 = 20.
4) Which loop visits every element of an array exactly once?
Answer: C. Indices 0..length-1.
5) What is true about the enhanced for loop with arrays?
Answer: B. Enhanced for loops give the element values, not indexes.
6) Which task usually requires an indexed loop rather than enhanced for?
Answer: D. Swapping needs indexes to access positions.
7) Which statement correctly creates an ArrayList<Integer>?
Answer: A. Use wrapper type
Integer (not int).8) For an ArrayList named list, which method returns the number of elements?
Answer: C. ArrayLists use
size().9) What is the value of list after this code executes?
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(1, 15);
Answer: B. Inserting at index 1 shifts elements right.
10) Which call removes the element at index i from an ArrayList named list?
Answer: D.
remove(index) removes the element at that index.11) Which best describes the relationship between arrays and ArrayLists?
Answer: A. Fixed vs resizable is the key difference.
12) In a 2D array int[][] m, what does m.length represent?
Answer: C.
m.length is the number of row arrays.13) Which expression gets the number of columns in row r of m?
Answer: B. Each row is a 1D array; its length is the column count for that row.
14) Which nested loop correctly traverses every element of a rectangular 2D array m?
Answer: D. Correct bounds: rows 0..m.length-1, cols 0..m[r].length-1.
15) Which is true about enhanced for loops with 2D arrays?
Answer: A. Outer loop variable is a row array; inner loop variable is an element.
Unit 4 Quiz — FRQ (1)
This FRQ uses a common Unit 4 pattern: traversal + conditional update.
FRQ 1) Replace negatives with 0 (Array)
Write a method public static void clampNegatives(int[] a) that updates the array
so that any negative value is replaced with 0. The method does not return anything.
Write your solution here:
Sample solution
public static void clampNegatives(int[] a) {
for (int i = 0; i < a.length; i++) {
if (a[i] < 0) {
a[i] = 0;
}
}
}
Uses an indexed loop because we need to write back into the array.