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.

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?

Topic 4.1

2) What is the valid index range for an array with length n?

Topic 4.1

3) What is printed?

Topic 4.2
int[] a = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < a.length; i++) {
  sum += a[i];
}
System.out.println(sum);

4) Which loop visits every element of an array exactly once?

Topic 4.2

5) What is true about the enhanced for loop with arrays?

Topic 4.3

6) Which task usually requires an indexed loop rather than enhanced for?

Topic 4.4

7) Which statement correctly creates an ArrayList<Integer>?

Topic 4.5

8) For an ArrayList named list, which method returns the number of elements?

Topic 4.5

9) What is the value of list after this code executes?

Topic 4.9
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(1, 15);

10) Which call removes the element at index i from an ArrayList named list?

Topic 4.9

11) Which best describes the relationship between arrays and ArrayLists?

Topic 4.10

12) In a 2D array int[][] m, what does m.length represent?

Topic 4.11

13) Which expression gets the number of columns in row r of m?

Topic 4.12

14) Which nested loop correctly traverses every element of a rectangular 2D array m?

Topic 4.13

15) Which is true about enhanced for loops with 2D arrays?

Topic 4.14

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: