---
title: "AP CSA Unit 4 Review: Data Collections | Fiveable"
description: "AP Computer Science A Unit 4 covers Ethical and Social Implications and Introduction to Using Data Sets. Study guides, practice questions, and key terms."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-4"
type: "unit"
subject: "AP Computer Science A"
unit: "Unit 4 – Data Collections"
---

# AP CSA Unit 4 Review: Data Collections | Fiveable

## Overview

Unit 4 covers every major data collection structure in Java: 1D arrays, ArrayLists, and 2D arrays, along with the algorithms that operate on them. You will trace and implement traversals, searching, sorting, and recursion, and you will also examine the ethical issues that arise when collecting and using data.

## AP CED Alignment

This unit hub is organized around AP Course and Exam Description topics, skills, and exam task types when they are available in the source data.
- 4.1: Ethical and Social Issues Around Data Collection
- 4.2: Introduction to Using Data Sets
- 4.3: Array Creation and Access
- 4.4: Array Traversals
- 4.5: Implementing Array Algorithms
- 4.6: Using Text Files
- 4.7: Wrapper Classes
- 4.8: ArrayList Methods
- 4.9: ArrayList Traversals
- 4.10: Implementing ArrayList Algorithms
- 4.11: 2D Array Creation and Access
- 4.12: 2D Array Traversals
- 4.13: Implementing 2D Array Algorithms
- 4.14: Searching Algorithms
- 4.15: Sorting Algorithms
- 4.16: Recursion
- 4.17: Recursive Searching and Sorting
- 4.11: 4.11 2D Arrays
- 4.5: 4.5 Developing Algorithms Using Arrays
- 4.1-4.2: Data Ethics and Data Sets
- 4.3-4.4: Array Creation, Access, and Traversal
- 4.5: Standard Array Algorithms
- 4.6-4.7: Text Files and Wrapper Classes
- 4.8-4.10: ArrayList Methods, Traversal, and Algorithms
- 4.11-4.13: 2D Arrays: Creation, Traversal, and Algorithms
- 4.14: Linear Search
- 4.15: Selection Sort and Insertion Sort
- 4.17: Binary Search and Merge Sort
- Practice 4 - Document Code and Computing Systems
- Practice 1 - Design Code
- FRQ 4 – 2D Array
- FRQ 3 – Array/ArrayList

## Topics

- [4.1: Ethical and Social Issues Around Data Collection](/ap-comp-sci-a/unit-4/ethical-and-social-implications/study-guide/iec7yzDQ2qENx5UAdiPJ): Privacy risks from storing personal data, algorithmic bias from flawed data sets, and how to evaluate whether a data set is appropriate for a given question.
- [4.2: Introduction to Using Data Sets](/ap-comp-sci-a/unit-4/introduction-to-using-data-sets/study-guide/fq4I4p0XINBBV56xQfTx): A data set is a collection of related information processed one element at a time. Charts and tables help plan the algorithm before writing code.
- [4.3: Array Creation and Access](/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi): 1D arrays store fixed-length collections of same-type values. Created with new or an initializer list; accessed with arr[i]; valid indices are 0 to arr.length - 1.
- [4.4: Array Traversals](/ap-comp-sci-a/unit-4/traversing-arrays/study-guide/kRcOqfawCcBz6gcT646t): Indexed for loops and while loops allow element modification; enhanced for loops provide read-only access. Assigning to the enhanced for variable does not change the array.
- [4.5: Implementing Array Algorithms](/ap-comp-sci-a/unit-4/implementing-2d-array-algorithms/study-guide/9ucC5cB6ffrLnA4b3FU3): Standard algorithms include min/max, sum/average, existential and universal checks, count, consecutive pair access, duplicate detection, shift, rotate, and reverse.
- [4.6: Using Text Files](/ap-comp-sci-a/unit-4/using-text-files/study-guide/JhiQXb5r4daUivzP1Xkm): Use File(String) and Scanner(File) to read persistent data. The method header must declare throws IOException. Use hasNext() in a while loop to read all tokens.
- [4.7: Wrapper Classes](/ap-comp-sci-a/unit-4/wrapper-classes-integer-double/study-guide/CKbVZpoW1SH2jmwauzDE): Integer and Double wrap primitives as objects. Autoboxing and unboxing happen automatically. Integer.parseInt() and Double.parseDouble() convert Strings to numbers.
- [4.8: ArrayList Methods](/ap-comp-sci-a/unit-4/arraylist-methods/study-guide/8juIkbLwLGELYtblBeCf): ArrayList is a dynamically sized list from java.util. Core methods: size(), add(E), add(int, E), get(int), set(int, E), remove(int). Use ArrayList<E> for compile-time type safety.
- [4.9: ArrayList Traversals](/ap-comp-sci-a/unit-4/traversing-arraylists/study-guide/U4SdcheNw5PMSIzjU2oL): Use indexed for loops when adding or removing elements during traversal. Enhanced for loops throw ConcurrentModificationException if the list size changes inside the loop.
- [4.10: Implementing ArrayList Algorithms](/ap-comp-sci-a/unit-4/developing-algorithms-using-arraylists/study-guide/MKbteieYvLOpWIwfqiND): Same standard algorithms as arrays: min, max, sum, average, count, property checks, duplicates, shift, rotate, reverse, insert, and delete. Decrement the index after removing an element.
- [4.11: 2D Array Creation and Access](/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi): A 2D array is an array of arrays. Created with new int[rows][cols] or a nested initializer list. Access with arr[row][col]; row count is arr.length, column count is arr[0].length.
- [4.12: 2D Array Traversals](/ap-comp-sci-a/unit-4/traversing-2d-arrays/study-guide/ZBcYqrg8FMJ49z1XpR8f): Nested for loops traverse in row-major or column-major order. The outer enhanced for variable is a 1D array (a row); the inner variable is the element type.
- [4.13: Implementing 2D Array Algorithms](/ap-comp-sci-a/unit-4/implementing-2d-array-algorithms/study-guide/9ucC5cB6ffrLnA4b3FU3): Apply standard algorithms to the full 2D array or to a specific row, column, or subsection. Track both row and column indices to process the correct region.
- [4.14: Searching Algorithms](/ap-comp-sci-a/unit-4/searching/study-guide/8PTb7wMUSzHeI7aTUFNy): Linear search checks each element in order and works on any data. For 2D arrays, apply linear search row by row. Return the index or -1 if not found.
- [4.15: Sorting Algorithms](/ap-comp-sci-a/unit-4/sorting/study-guide/P5PACxSTKavEy6V3x3Nt): Selection sort swaps the minimum of the unsorted portion into its final position each pass. Insertion sort shifts elements to insert each new value into the sorted prefix.
- [4.16: Recursion](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4): A recursive method calls itself with a base case to stop and a recursive call that moves toward it. Each call has its own local variables. Trace calls from the base case outward. Writing recursion is outside exam scope.
- [4.17: Recursive Searching and Sorting](/ap-comp-sci-a/unit-4/recursive-searching-and-sorting/study-guide/tP6n1uldmjMrgteAVspr): Binary search requires sorted data and halves the search space each step. Merge sort splits to single elements then merges in order. Both run more efficiently than linear search and O(n^2) sorts on large inputs.
- [4.11: 4.11 2D Arrays](/ap-comp-sci-a/unit-4/2d-arrays/study-guide/5WDx6ZFeWhx2aVuiZI6R): Review 2D arrays in AP Computer Science A, including Java int[][] syntax, arr[row][col], default values, initializer lists, and length.
- [4.5: 4.5 Developing Algorithms Using Arrays](/ap-comp-sci-a/unit-4/developing-algorithms-using-arrays/study-guide/c6dpJfmjG7oVFDqnXFAk): Review AP CSA array algorithms, including traversals, min/max, sums, counting, duplicate checks, consecutive pairs, shifting, rotating, and reversing arrays.

## Hardest Topics And Analytics

Snapshot: practice snapshot
This snapshot uses Fiveable practice activity to show where students tend to miss questions and which review moves are worth prioritizing first.
- **66% average MCQ accuracy** (Across 6.9k multiple-choice practice attempts for this unit.)
- **6.9k MCQ attempts** (Practice activity included in this snapshot.)

## Review Notes

### 4.1-4.2: Data Ethics and Data Sets

Before writing any code, AP CSA asks you to think about the human side of data. Topic 4.1 covers privacy risks and algorithmic bias; Topic 4.2 introduces the idea of a data set and how to plan algorithms that process it.

- **Personally identifiable information (PII)**: Data that can identify a specific individual; programmers must protect it and minimize what they collect.
- **Algorithmic bias**: Systemic errors in a program that produce unfair outcomes for a specific group, often caused by biased or incomplete training data.
- **Data set fitness**: A data set must match the question being asked; using an unrelated or incomplete data set leads to incorrect conclusions.
- **Element-wise processing**: Data sets are analyzed by accessing and processing one value at a time, then aggregating results such as sums, counts, or averages.
- **Diagram planning**: Charts and tables can represent a data set visually and help plan the algorithm before writing code.

**Checkpoint:** Can you explain two ways algorithmic bias can enter a program, and describe what makes a data set appropriate for a given question?

Issue | Cause | Programmer response
--- | --- | ---
Privacy breach | Storing unnecessary PII | Collect only what is needed; protect stored data
Algorithmic bias | Biased or unrepresentative training data | Audit data collection method before use
Incomplete data | Missing records or fields | Verify data set completeness before drawing conclusions

### 4.3-4.4: Array Creation, Access, and Traversal

A 1D array stores multiple values of the same type under one variable. Its length is fixed at creation. You access elements with arr[i], where valid indices run from 0 to arr.length - 1. Traversal uses an indexed for loop, a while loop, or an enhanced for loop depending on whether you need to modify elements.

- **Default values**: When created with new, int elements default to 0, double to 0.0, boolean to false, and reference types to null.
- **ArrayIndexOutOfBoundsException**: Thrown at runtime when an index is negative or greater than or equal to arr.length.
- **Enhanced for loop limitation**: Assigning a new value to the enhanced for loop variable does not change the array; use an indexed loop to modify elements.
- **Object references in arrays**: When an array stores objects, the enhanced for loop variable holds a reference; you can call methods on it to change the object's state.
- **Initializer list**: Syntax like int[] nums = {3, 7, 2} creates and populates an array in one statement without using new.

**Checkpoint:** Trace this loop: int[] a = {4, 1, 3}; for (int i = a.length - 1; i >= 0; i--) System.out.print(a[i] + " "); What prints?

Loop type | Can modify elements? | Needs index? | Best use
--- | --- | --- | ---
Indexed for loop | Yes | Yes | Modify elements, access by position
While loop | Yes | Yes | Variable step size or early exit
Enhanced for loop | No (primitives) | No | Read-only traversal of all elements

### 4.5: Standard Array Algorithms

Every standard array algorithm is a traversal with a specific accumulation or comparison pattern. Know how to implement and trace each one.

- **Min/max tracking**: Initialize a variable to the first element, then update it whenever a smaller or larger element is found during traversal.
- **Sum and average**: Accumulate a running total in a loop; divide by arr.length after the loop for the average.
- **Existential and universal checks**: Use a boolean flag or early return: any-element checks return true on first match; all-element checks return false on first failure.
- **Shift and rotate**: Shifting moves elements left or right by one position; rotating wraps the displaced element to the other end.
- **Reverse in place**: Swap arr[i] and arr[arr.length - 1 - i] for i from 0 to arr.length / 2 - 1.

**Checkpoint:** Write the loop condition and body to find the index of the maximum value in an int array named scores.

### 4.6-4.7: Text Files and Wrapper Classes

Topic 4.6 covers reading persistent data from a file using File and Scanner. Topic 4.7 introduces Integer and Double wrapper classes, which are required when storing primitives in an ArrayList.

- **File and Scanner pattern**: Create a File object with the filename string, pass it to a Scanner constructor, then use hasNext() and nextInt()/nextDouble()/next() in a while loop.
- **throws IOException**: Any method that opens a file must declare throws IOException in its header; if the file is not found, the program terminates.
- **Autoboxing**: Java automatically converts a primitive int or double to its wrapper Integer or Double when the context requires an object, such as adding to an ArrayList.
- **Unboxing**: Java automatically converts an Integer or Double back to its primitive when the context requires a primitive, such as arithmetic.
- **Integer.parseInt / Double.parseDouble**: Static methods that convert a String to an int or double; useful when reading numeric data from a file as text.

**Checkpoint:** What two import statements are needed to read from a file, and what happens if the file name passed to the File constructor does not exist?

Wrapper class | Primitive | Parse method | Immutable?
--- | --- | --- | ---
Integer | int | Integer.parseInt(String) | Yes
Double | double | Double.parseDouble(String) | Yes

### 4.8-4.10: ArrayList Methods, Traversal, and Algorithms

An ArrayList is a dynamically sized list of object references from java.util. Its five core methods are size(), add(E), add(int, E), get(int), set(int, E), and remove(int). Traversal and algorithms follow the same patterns as arrays but require careful handling when the list size changes during a loop.

- **Generic type ArrayList<E>**: Specifying the element type, such as ArrayList<String>, lets the compiler catch type errors at compile time instead of runtime.
- **remove(int) shifting**: Removing an element shifts all later elements left by one; if you remove inside an indexed for loop, decrement i after the removal to avoid skipping an element.
- **ConcurrentModificationException**: Thrown when you add or remove elements inside an enhanced for loop over an ArrayList; use an indexed for loop instead.
- **set() return value**: set(int index, E element) replaces the element at index and returns the element that was previously stored there.
- **Multi-list algorithms**: Some algorithms require traversing two ArrayLists simultaneously, such as merging two sorted lists or comparing corresponding elements.

**Checkpoint:** An ArrayList<Integer> contains [5, 3, 8, 3, 2]. Write the loop that removes all elements equal to 3 without skipping any element.

Method | Returns | Effect on size
--- | --- | ---
size() | int | None
add(E) | void | Increases by 1
add(int, E) | void | Increases by 1, shifts right
remove(int) | E (removed element) | Decreases by 1, shifts left
set(int, E) | E (previous element) | None

### 4.11-4.13: 2D Arrays: Creation, Traversal, and Algorithms

A 2D array is an array of arrays. You create it with new int[rows][cols] or an initializer list, access elements with arr[row][col], get the row count from arr.length, and the column count from arr[0].length. Nested loops traverse in row-major or column-major order.

- **Row-major vs column-major order**: Row-major: outer loop over rows, inner loop over columns. Column-major: outer loop over columns, inner loop over rows. The order matters when the algorithm depends on sequence.
- **Enhanced for loop over 2D array**: The outer enhanced for variable has type int[] (a row); the inner enhanced for variable has the element type. Assigning to the inner variable does not change the array.
- **arr.length and arr[0].length**: arr.length gives the number of rows; arr[0].length gives the number of columns in the first row.
- **Subsection algorithms**: Standard algorithms like min, max, sum, and count can be applied to the entire 2D array or restricted to a specific row, column, or rectangular region.
- **Default values in 2D arrays**: Same as 1D: int defaults to 0, double to 0.0, boolean to false, reference types to null.

**Checkpoint:** Write nested for loops that compute the sum of all elements in a 2D int array named grid with r rows and c columns.

Traversal order | Outer loop variable | Inner loop variable | Typical use
--- | --- | --- | ---
Row-major | row index i | column index j | Process each row left to right
Column-major | column index j | row index i | Process each column top to bottom

### 4.14: Linear Search

Linear search checks each element in order until the target is found or the collection is exhausted. It works on unsorted and sorted data and can start from either end. For 2D arrays, apply linear search row by row using nested loops.

- **Return convention**: Return the index where the target was found, or -1 if it was not found.
- **Boolean flag pattern**: An alternative to returning an index: set a boolean found = false before the loop and set it to true when the target is located.
- **2D linear search**: Use an outer loop over rows and an inner loop over columns; check arr[i][j] against the target at each position.
- **No sorted-order requirement**: Unlike binary search, linear search does not require the data to be sorted.
- **Early termination**: Once the target is found, the loop can exit immediately using a return statement or break to avoid unnecessary comparisons.

**Checkpoint:** Trace a linear search for the value 7 in the array [2, 9, 7, 4, 1]. How many comparisons are made before the value is found?

### 4.15: Selection Sort and Insertion Sort

Both algorithms sort in place and run in O(n^2) time. Selection sort minimizes swaps; insertion sort is efficient on nearly sorted data. The exam asks you to trace each pass, not write the full algorithm from scratch.

- **Selection sort pass**: Find the index of the minimum value in the unsorted portion, then swap it with the first element of the unsorted portion. Repeat until sorted.
- **Insertion sort pass**: Take the next element from the unsorted portion as the key, then shift sorted elements right until the correct position for the key is found, and insert it.
- **Sorted vs unsorted portion**: Both algorithms maintain a growing sorted prefix and a shrinking unsorted suffix. After k passes, the first k elements are in their correct relative positions.
- **Stability**: Insertion sort is stable (equal elements keep their original order); selection sort is not guaranteed to be stable.
- **Tracing tip**: Write out the array after each outer-loop pass to estimate score progress; the exam often asks for the state after a specific number of passes.

**Checkpoint:** Trace selection sort on [5, 2, 8, 1, 4]. What does the array look like after two complete passes?

Algorithm | Key operation | Swaps per pass | Best case
--- | --- | --- | ---
Selection sort | Find min, swap into position | Exactly 1 | O(n^2) comparisons
Insertion sort | Shift elements, insert key | 0 to many | O(n) when already sorted

### 4.16: Recursion

A recursive method calls itself. Every recursive method needs at least one base case that stops the recursion and at least one recursive call that moves toward the base case. On the AP exam, you trace recursive calls and determine the return value; writing recursive code is outside exam scope.

- **Base case**: The condition under which the method returns a value directly without making another recursive call; prevents infinite recursion.
- **Recursive call**: The call to the same method with a modified argument that moves closer to the base case.
- **Local variables per call**: Each recursive call has its own set of local variables and parameters on the call stack, independent of other calls.
- **Tracing technique**: Work from the innermost base case outward, substituting return values back up the call chain to find the final result.
- **Recursion vs iteration**: Any recursive solution can be rewritten iteratively and vice versa; parameter values in recursion play the same role as loop control variables.

**Checkpoint:** Trace mystery(4) where mystery(int n) returns 0 if n == 0, else returns n + mystery(n - 1). What is the return value?

### 4.17: Binary Search and Merge Sort

Topic 4.17 applies recursion to searching and sorting. Binary search requires sorted data and eliminates half the collection each step. Merge sort recursively splits a collection to single elements and merges them back in order. Both can be written iteratively or recursively; the exam asks you to trace them.

- **Binary search precondition**: The array or ArrayList must be sorted before binary search is applied; using it on unsorted data produces incorrect results.
- **Mid-index calculation**: mid = (low + high) / 2; compare the target to arr[mid] and eliminate the left or right half accordingly.
- **Merge sort split phase**: Recursively divide the collection in half until each subarray contains one element, which is trivially sorted.
- **Merge sort merge phase**: Combine two sorted subarrays into one sorted array by comparing front elements and placing the smaller one first.
- **Efficiency comparison**: Binary search is more efficient than linear search on sorted data; merge sort runs in O(n log n) time, faster than O(n^2) selection and insertion sort for large inputs.

**Checkpoint:** Trace binary search for the value 14 in [2, 5, 8, 11, 14, 17, 20]. List the mid value checked at each step.

Algorithm | Precondition | Strategy | Time complexity
--- | --- | --- | ---
Linear search | None | Check each element in order | O(n)
Binary search | Sorted data | Eliminate half each step | O(log n)
Selection sort | None | Swap min into place each pass | O(n^2)
Insertion sort | None | Insert each element into sorted prefix | O(n^2) worst, O(n) best
Merge sort | None | Split then merge recursively | O(n log n)

## Study Guides

- [4.6 Using Text Files](/ap-comp-sci-a/unit-4/using-text-files/study-guide/JhiQXb5r4daUivzP1Xkm)
- [4.12 Traversing 2D Arrays](/ap-comp-sci-a/unit-4/traversing-2d-arrays/study-guide/ZBcYqrg8FMJ49z1XpR8f)
- [4.3 Array Creation and Access](/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi)
- [4.16 Recursion](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4)
- [4.1 Ethical and Social Implications](/ap-comp-sci-a/unit-4/ethical-and-social-implications/study-guide/iec7yzDQ2qENx5UAdiPJ)
- [4.14 Searching](/ap-comp-sci-a/unit-4/searching/study-guide/8PTb7wMUSzHeI7aTUFNy)
- [4.17 Recursive Searching and Sorting](/ap-comp-sci-a/unit-4/recursive-searching-and-sorting/study-guide/tP6n1uldmjMrgteAVspr)
- [4.9 Traversing ArrayLists](/ap-comp-sci-a/unit-4/traversing-arraylists/study-guide/U4SdcheNw5PMSIzjU2oL)
- [4.11 2D Arrays](/ap-comp-sci-a/unit-4/2d-arrays/study-guide/5WDx6ZFeWhx2aVuiZI6R)
- [4.4 Traversing Arrays](/ap-comp-sci-a/unit-4/traversing-arrays/study-guide/kRcOqfawCcBz6gcT646t)
- [4.15 Sorting](/ap-comp-sci-a/unit-4/sorting/study-guide/P5PACxSTKavEy6V3x3Nt)
- [4.5 Developing Algorithms Using Arrays](/ap-comp-sci-a/unit-4/developing-algorithms-using-arrays/study-guide/c6dpJfmjG7oVFDqnXFAk)
- [4.13 Implementing 2D Array Algorithms](/ap-comp-sci-a/unit-4/implementing-2d-array-algorithms/study-guide/9ucC5cB6ffrLnA4b3FU3)
- [4.2 Introduction to Using Data Sets](/ap-comp-sci-a/unit-4/introduction-to-using-data-sets/study-guide/fq4I4p0XINBBV56xQfTx)
- [4.8 ArrayList Methods](/ap-comp-sci-a/unit-4/arraylist-methods/study-guide/8juIkbLwLGELYtblBeCf)
- [4.7 Wrapper Classes: Integer and Double](/ap-comp-sci-a/unit-4/wrapper-classes-integer-double/study-guide/CKbVZpoW1SH2jmwauzDE)
- [4.10 Developing Algorithms Using ArrayLists](/ap-comp-sci-a/unit-4/developing-algorithms-using-arraylists/study-guide/MKbteieYvLOpWIwfqiND)

## Practice Preview

### Multiple-choice practice

- **AP-style practice question**: Practice 4 - Document Code and Computing Systems | Consider the following method that processes an `ArrayList<Integer>`.

```java
public static void processList(ArrayList<Integer> data) {
    int first = data.get(0);
    data.add(first);
    data.remove(0);
}
```

Which of the following describes the initial condition that must be met for this method to execute without throwing an exception?
- **AP-style practice question**: Practice 1 - Design Code | A coffee shop is designing a `SalesTracker` class to determine if playing classical music increases the sales of premium espresso drinks compared to playing pop music. Which of the following sets of instance variables provides the most appropriate data set?
- **AP-style practice question**: Practice 1 - Design Code | A factory manager is designing a `QualityControl` class to determine if a new machine calibration reduces the number of defective products produced per hour. Which of the following sets of instance variables provides the most appropriate data set?
- **AP-style practice question**: Practice 1 - Design Code | A meteorologist is writing a program to determine if average summer temperatures in a specific city have increased over the last 50 years. Which set of instance variables in the `ClimateData` class represents the most appropriate data set?
- **AP-style practice question**: Practice 1 - Design Code | A streaming service wants to determine if releasing all episodes of a show at once (binge model) results in higher total viewership than releasing one episode per week (weekly model). Which method signature collects the most appropriate data for this analysis?
- **AP-style practice question**: Practice 1 - Design Code | A streaming music service collects the following datasets:
- Dataset 1: Anonymized listening logs containing a unique user ID, the names of songs played, and the exact timestamps of when each song was played.
- Dataset 2: A public database containing the names of songs, their artists, their genres, and their total global play counts.
- Dataset 3: A leaked database containing unique user IDs, user full names, and their associated email addresses.
- Dataset 4: Anonymized payment logs containing a unique user ID, the subscription tier purchased, and the date of the transaction.
Which combination of datasets would allow a malicious actor to determine the full names of users who listened to a specific genre of music?

### FRQ practice

- **2D array traversal, row weight calculation**: FRQ 4 – 2D Array | 2D array traversal, row weight calculation
- **Temperature averaging with conditional filtering criteria**: FRQ 3 – Array/ArrayList | Temperature averaging with conditional filtering criteria

## Key Terms

- **Traversal**: The process of visiting and accessing each element in a data structure such as an array or ArrayList in a specific order, typically using a for loop or while loop.
- **object reference**: A memory address that a reference-type variable holds, allowing it to access an object stored in memory. ArrayLists store object references, not primitive values directly.
- **generic type**: A Java feature using angle brackets to specify the element type of a collection; for example, ArrayList<String> restricts the list to String objects and enables compile-time type checking.
- **out-of-bounds error**: An error thrown at runtime when code attempts to access an index that is negative or greater than or equal to the length of an array or the size of an ArrayList.
- **file**: Storage for data that persists when a program is not running. In Java, a File object and a Scanner are used together to read data from a text file during program execution.
- **row index**: The first index in arr[row][col] notation for a 2D array, specifying which row the element is located in. The number of rows is given by arr.length.
- **column index**: The second index in arr[row][col] notation for a 2D array, specifying which column the element is located in. The number of columns is given by arr[0].length.
- **.get() method**: An ArrayList method that retrieves the element at a specified index. Syntax: list.get(i) returns the element at index i without removing it.
- **maximum tracking**: A standard algorithm pattern that initializes a variable to the first element and updates it whenever a larger element is found during traversal of an array or ArrayList.
- **shift algorithm**: A standard algorithm that moves elements within an array or 2D array row in a specified direction, with the displaced element either lost or wrapped to the other end.
- **duplicate elements**: An algorithm task that checks whether an array or ArrayList contains repeated values, typically using nested loops or a comparison of each element against all others.
- **boolean array**: An array whose elements are of type boolean, initialized to false by default when created with new. Useful for tracking which elements satisfy a condition.

## Common Mistakes

- **Off-by-one errors in array and ArrayList loops**: Valid array indices are 0 to arr.length - 1, and valid ArrayList indices are 0 to list.size() - 1. Using <= arr.length or <= list.size() as the loop condition throws an out-of-bounds exception on the last iteration.
- **Modifying an ArrayList inside an enhanced for loop**: Adding or removing elements inside an enhanced for loop over an ArrayList causes a ConcurrentModificationException. Use an indexed for loop and adjust the index variable after a removal.
- **Assuming the enhanced for loop can update array elements**: Assigning a new value to the enhanced for loop variable only changes the local copy; the array element is unchanged. Use an indexed loop with arr[i] = newValue to actually modify the array.
- **Applying binary search to unsorted data**: Binary search only works correctly when the collection is already sorted. Applying it to unsorted data produces wrong results without throwing an exception, making the bug hard to detect.
- **Confusing arr.length with arr.length() for arrays**: Arrays use the length attribute without parentheses: arr.length. Strings use the length() method with parentheses. Mixing these up causes a compile-time error.

## Exam Connections

- **Code tracing on multiple-choice questions**: A large portion of AP CSA multiple-choice questions present a short method that traverses an array, ArrayList, or 2D array and ask what value is returned or printed. Practice tracing loop variables, index values, and accumulator variables step by step. Pay close attention to loop bounds, the difference between < and <=, and what happens when remove() is called inside a loop.
- **Free-response algorithm implementation**: Free-response questions frequently ask you to write a method that processes an array or ArrayList using a standard algorithm pattern such as finding a maximum, filtering elements, or comparing consecutive pairs. You are expected to use correct Java syntax, choose the right loop type, and handle edge cases like an empty list or a single-element array. Wrapper classes and autoboxing appear when the method works with ArrayList<Integer> or ArrayList<Double>.
- **Algorithm tracing for searching and sorting**: Questions on searching and sorting ask you to trace the state of a collection after a specific number of passes or iterations. For binary search, you identify the mid index and which half is eliminated at each step. For selection sort and insertion sort, you show the array after each outer-loop pass. For merge sort, you trace the split and merge phases. Knowing the sorted-order precondition for binary search is a common discrimination point.

## Final Review Checklist

- **Final Unit 4 review checklist: Arrays**: Create a 1D array with new and with an initializer list. Access and modify elements using arr[i]. Trace indexed for loops, while loops, and enhanced for loops. Implement min, max, sum, shift, and reverse algorithms.
- **Final Unit 4 review checklist: ArrayLists**: Declare an ArrayList<E>, call size(), add(), get(), set(), and remove() correctly. Trace what happens to indices when an element is removed. Avoid ConcurrentModificationException by using an indexed loop when modifying the list.
- **Final Unit 4 review checklist: 2D Arrays**: Create a 2D array and access elements with arr[row][col]. Use arr.length and arr[0].length for loop bounds. Write nested loops for row-major and column-major traversal. Apply standard algorithms to rows, columns, and the full array.
- **Final Unit 4 review checklist: Files and Wrappers**: Write the File and Scanner constructor calls needed to open a file. Add throws IOException to the method header. Explain autoboxing and unboxing. Use Integer.parseInt() and Double.parseDouble() to convert file data.
- **Final Unit 4 review checklist: Searching and Sorting**: Trace linear search on an array, ArrayList, and 2D array. Trace selection sort and insertion sort pass by pass. State the sorted-order precondition for binary search and trace each step of a binary search.
- **Final Unit 4 review checklist: Recursion and Merge Sort**: Identify the base case and recursive call in a given recursive method. Trace the call stack to find the return value. Trace merge sort through the split and merge phases. Explain why binary search and merge sort are more efficient than their linear counterparts.
- **Final Unit 4 review checklist: Data Ethics**: Explain two privacy risks from storing personal data. Define algorithmic bias and give an example of how biased data causes it. Evaluate whether a given data set is appropriate for a specific question.

## Study Plan

- **Step 1: Data ethics, data sets, and arrays (4.1-4.5)**: Read the topic guides for 4.1 through 4.5. Practice explaining algorithmic bias in your own words. Then write and trace array creation, access, and the standard traversal algorithms: min, max, sum, shift, and reverse. Use the available practice questions to check your index arithmetic.
- **Step 2: Files, wrapper classes, and ArrayLists (4.6-4.10)**: Work through the topic guides for 4.6 and 4.7 to understand file reading and autoboxing. Then study ArrayList methods in 4.8, practice the removal-during-traversal pattern from 4.9, and implement the standard ArrayList algorithms from 4.10. Compare ArrayList and array behavior side by side.
- **Step 3: 2D arrays (4.11-4.13)**: Review 2D array creation and indexing with the topic guide for 4.11. Practice writing nested loops for row-major and column-major traversal from 4.12. Then apply standard algorithms to rows, columns, and subsections as covered in 4.13. Trace code that uses arr.length and arr[0].length for loop bounds.
- **Step 4: Searching and sorting (4.14-4.15)**: Trace linear search on arrays, ArrayLists, and 2D arrays. Then trace selection sort and insertion sort pass by pass on short arrays. Use the topic guides for 4.14 and 4.15 and work through practice questions that ask for the array state after a specific number of passes.
- **Step 5: Recursion and recursive algorithms (4.16-4.17)**: Study the recursion topic guide for 4.16 and practice tracing call stacks for factorial-style and string-processing examples. Then move to 4.17: trace binary search iterations and merge sort split-and-merge phases. Use the AP score calculator to estimate your readiness after completing practice questions across the full unit.

## More Ways To Review

- [Topic study guides](/ap-comp-sci-a/unit-4#topics)
- [FRQ practice](/ap-comp-sci-a/frq-practice)
- [Cheatsheets](/ap-comp-sci-a/cheatsheets/unit-4)
- [Key terms](/ap-comp-sci-a/key-terms)

## FAQs

### What topics are covered in AP CSA Unit 4?

AP CSA Unit 4 covers 17 topics across arrays, ArrayLists, 2D arrays, and algorithms. Key topics include Array Creation and Access, Array Traversals, ArrayList Methods, ArrayList Traversals, 2D Array Creation and Access, 2D Array Traversals, Searching Algorithms, Sorting Algorithms, Recursion, and Recursive Searching and Sorting. It also covers Wrapper Classes, Using Text Files, and Ethical and Social Issues Around Data Collection. See the full topic list at [/ap-comp-sci-a/unit-4](/ap-comp-sci-a/unit-4).

### How much of the AP CSA exam is Unit 4?

AP CSA Unit 4 makes up 30-40% of the AP exam, making it the heaviest-weighted unit on the test. It covers arrays, ArrayLists, 2D arrays, searching and sorting algorithms, and recursion. Because so much of the exam draws from this unit, getting comfortable with these data structures and their algorithms is one of the highest-leverage things you can do.

### What's on the AP CSA Unit 4 progress check (MCQ and FRQ)?

The AP CSA Unit 4 progress check includes both MCQ and FRQ parts that test your understanding of arrays, ArrayLists, 2D arrays, searching and sorting algorithms, and recursion. The MCQ section tests topics like Array Traversals, ArrayList Methods, and 2D Array Traversals. The FRQ section asks you to write or analyze code using these data structures and algorithms, similar to what you'll see on the real AP exam. For matched practice on all 17 Unit 4 topics, visit [/ap-comp-sci-a/unit-4](/ap-comp-sci-a/unit-4).

### How do I practice AP CSA Unit 4 FRQs?

AP CSA Unit 4 FRQs most often involve writing methods that use arrays, ArrayLists, 2D arrays, or recursion to solve a problem. You'll typically be asked to traverse a data structure, implement a searching or sorting algorithm, or write a recursive method. To practice, work through released College Board FRQs that focus on Array Algorithms, ArrayList Algorithms, and 2D Array Algorithms, then check your logic step by step. Find practice problems and study guides at [/ap-comp-sci-a/unit-4](/ap-comp-sci-a/unit-4).

### Where can I find AP CSA Unit 4 practice questions?

You can find AP CSA Unit 4 MCQ and practice test questions at [/ap-comp-sci-a/unit-4](/ap-comp-sci-a/unit-4), which has resources organized by topic across all 17 Unit 4 topics. For targeted multiple-choice practice, focus on Array Traversals, ArrayList Methods, 2D Array Traversals, Searching Algorithms, Sorting Algorithms, and Recursion, since those topics show up most often on the exam.

### How should I study AP CSA Unit 4?

Start with arrays before moving to ArrayLists, since ArrayLists build on the same traversal logic but add dynamic sizing. Then move to 2D arrays, which are just arrays of arrays. Once your data structure fundamentals are solid, tackle Searching Algorithms and Sorting Algorithms, then finish with Recursion and Recursive Searching and Sorting. Writing actual code for each topic, not just reading it, is the fastest way to build real understanding. - Review Array Creation and Access, then practice traversal loops.
- Learn ArrayList Methods like `add`, `remove`, `get`, and `size`.
- Practice 2D Array Traversals with nested loops.
- Trace through linear search, binary search, selection sort, and insertion sort by hand.
- Write small recursive methods and identify the base case and recursive case every time. All 17 topics with study guides are at [/ap-comp-sci-a/unit-4](/ap-comp-sci-a/unit-4).

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#what-topics-are-covered-in-ap-csa-unit-4","name":"What topics are covered in AP CSA Unit 4?","acceptedAnswer":{"@type":"Answer","text":"AP CSA Unit 4 covers 17 topics across arrays, ArrayLists, 2D arrays, and algorithms. Key topics include Array Creation and Access, Array Traversals, ArrayList Methods, ArrayList Traversals, 2D Array Creation and Access, 2D Array Traversals, Searching Algorithms, Sorting Algorithms, Recursion, and Recursive Searching and Sorting. It also covers Wrapper Classes, Using Text Files, and Ethical and Social Issues Around Data Collection. See the full topic list at <a href=\"/ap-comp-sci-a/unit-4\">/ap-comp-sci-a/unit-4</a>."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#how-much-of-the-ap-csa-exam-is-unit-4","name":"How much of the AP CSA exam is Unit 4?","acceptedAnswer":{"@type":"Answer","text":"AP CSA Unit 4 makes up 30-40% of the AP exam, making it the heaviest-weighted unit on the test. It covers arrays, ArrayLists, 2D arrays, searching and sorting algorithms, and recursion. Because so much of the exam draws from this unit, getting comfortable with these data structures and their algorithms is one of the highest-leverage things you can do."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#whats-on-the-ap-csa-unit-4-progress-check-mcq-and-frq","name":"What's on the AP CSA Unit 4 progress check (MCQ and FRQ)?","acceptedAnswer":{"@type":"Answer","text":"The AP CSA Unit 4 progress check includes both MCQ and FRQ parts that test your understanding of arrays, ArrayLists, 2D arrays, searching and sorting algorithms, and recursion. The MCQ section tests topics like Array Traversals, ArrayList Methods, and 2D Array Traversals. The FRQ section asks you to write or analyze code using these data structures and algorithms, similar to what you'll see on the real AP exam. For matched practice on all 17 Unit 4 topics, visit <a href=\"/ap-comp-sci-a/unit-4\">/ap-comp-sci-a/unit-4</a>."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#how-do-i-practice-ap-csa-unit-4-frqs","name":"How do I practice AP CSA Unit 4 FRQs?","acceptedAnswer":{"@type":"Answer","text":"AP CSA Unit 4 FRQs most often involve writing methods that use arrays, ArrayLists, 2D arrays, or recursion to solve a problem. You'll typically be asked to traverse a data structure, implement a searching or sorting algorithm, or write a recursive method. To practice, work through released College Board FRQs that focus on Array Algorithms, ArrayList Algorithms, and 2D Array Algorithms, then check your logic step by step. Find practice problems and study guides at <a href=\"/ap-comp-sci-a/unit-4\">/ap-comp-sci-a/unit-4</a>."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#where-can-i-find-ap-csa-unit-4-practice-questions","name":"Where can I find AP CSA Unit 4 practice questions?","acceptedAnswer":{"@type":"Answer","text":"You can find AP CSA Unit 4 MCQ and practice test questions at <a href=\"/ap-comp-sci-a/unit-4\">/ap-comp-sci-a/unit-4</a>, which has resources organized by topic across all 17 Unit 4 topics. For targeted multiple-choice practice, focus on Array Traversals, ArrayList Methods, 2D Array Traversals, Searching Algorithms, Sorting Algorithms, and Recursion, since those topics show up most often on the exam."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4#how-should-i-study-ap-csa-unit-4","name":"How should I study AP CSA Unit 4?","acceptedAnswer":{"@type":"Answer","text":"Start with arrays before moving to ArrayLists, since ArrayLists build on the same traversal logic but add dynamic sizing. Then move to 2D arrays, which are just arrays of arrays. Once your data structure fundamentals are solid, tackle Searching Algorithms and Sorting Algorithms, then finish with Recursion and Recursive Searching and Sorting. Writing actual code for each topic, not just reading it, is the fastest way to build real understanding. - Review Array Creation and Access, then practice traversal loops.\n- Learn ArrayList Methods like `add`, `remove`, `get`, and `size`.\n- Practice 2D Array Traversals with nested loops.\n- Trace through linear search, binary search, selection sort, and insertion sort by hand.\n- Write small recursive methods and identify the base case and recursive case every time. All 17 topics with study guides are at <a href=\"/ap-comp-sci-a/unit-4\">/ap-comp-sci-a/unit-4</a>."}}]}
```
