AP exam review verified for 2027

AP Computer Science A Unit 4 Review: Data Collections

Review AP CSA Unit 4 to build fluency with arrays, ArrayLists, 2D arrays, searching, sorting, and recursion. This unit carries 30-40% of the exam and demands both code-tracing accuracy and algorithm implementation skill.

Use the topic guides, key terms, and practice questions available on Fiveable to work through every concept before exam day.

What is AP Computer Science A unit 4?

Unit 4 is the largest unit in AP CSA by exam weight. It introduces three data structures for storing groups of related values, arrays, ArrayLists, and 2D arrays, and then builds a library of standard algorithms on top of them. The unit also opens with ethical questions about privacy and bias that apply whenever programs collect and process real data.

Unit 4 teaches you how to store, access, traverse, search, sort, and recursively process collections of data in Java, while also asking you to think critically about the social implications of data collection.

Three core data structures

A 1D array has a fixed length set at creation and uses arr[i] indexing. An ArrayList is dynamically sized and uses methods like add(), remove(), get(), and set(). A 2D array is an array of arrays accessed with arr[row][col] and traversed with nested loops.

Standard algorithms

Every data structure supports the same family of algorithms: find min or max, compute sum or average, count or check elements for a property, detect duplicates, shift or reverse elements, and insert or delete. Knowing these patterns for arrays, ArrayLists, and 2D arrays is the core skill of the unit.

Searching, sorting, and recursion

Linear search checks elements one at a time. Binary search cuts a sorted collection in half each step. Selection sort swaps the minimum into place; insertion sort shifts elements to insert each new value. Merge sort recursively splits and merges. Recursion itself requires tracing base cases and recursive calls through a call stack.

Why data collections handle the exam

Arrays, ArrayLists, and 2D arrays are the building blocks of almost every non-trivial Java program. The AP exam tests whether you can read code that manipulates these structures, predict its output, identify bugs, and write new algorithms. Because this unit accounts for 30-40% of the exam, a strong grasp of traversal patterns, index arithmetic, and algorithm tracing pays off across both the multiple-choice and free-response sections.

AP Computer Science A unit 4 topics

4.1

Ethical and Social Issues Around Data Collection

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.

open guide
4.2

Introduction to Using Data Sets

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.

open guide
4.3

Array Creation and Access

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.

open guide
4.4

Array Traversals

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.

open guide
4.5

Implementing Array Algorithms

Standard algorithms include min/max, sum/average, existential and universal checks, count, consecutive pair access, duplicate detection, shift, rotate, and reverse.

open guide
4.6

Using Text Files

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.

open guide
4.7

Wrapper Classes

Integer and Double wrap primitives as objects. Autoboxing and unboxing happen automatically. Integer.parseInt() and Double.parseDouble() convert Strings to numbers.

open guide
4.8

ArrayList Methods

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.

open guide
4.9

ArrayList Traversals

Use indexed for loops when adding or removing elements during traversal. Enhanced for loops throw ConcurrentModificationException if the list size changes inside the loop.

open guide
4.10

Implementing ArrayList Algorithms

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.

open guide
4.11

2D Array Creation and Access

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.

open guide
4.12

2D Array Traversals

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.

open guide
4.13

Implementing 2D Array Algorithms

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.

open guide
4.14

Searching Algorithms

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.

open guide
4.15

Sorting Algorithms

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.

open guide
4.16

Recursion

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.

open guide
4.17

Recursive Searching and Sorting

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.

open guide
4.11

4.11 2D Arrays

Review 2D arrays in AP Computer Science A, including Java int[][] syntax, arr[row][col], default values, initializer lists, and length.

open guide
4.5

4.5 Developing Algorithms Using Arrays

Review AP CSA array algorithms, including traversals, min/max, sums, counting, duplicate checks, consecutive pairs, shifting, rotating, and reversing arrays.

open guide
practice snapshot

Hardest AP Computer A unit 4 topics

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.9kMCQ attempts

Practice activity included in this snapshot.

Unit 4 review notes

4.1

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.
Can you explain two ways algorithmic bias can enter a program, and describe what makes a data set appropriate for a given question?
IssueCauseProgrammer response
Privacy breachStoring unnecessary PIICollect only what is needed; protect stored data
Algorithmic biasBiased or unrepresentative training dataAudit data collection method before use
Incomplete dataMissing records or fieldsVerify data set completeness before drawing conclusions
4.3

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.
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 typeCan modify elements?Needs index?Best use
Indexed for loopYesYesModify elements, access by position
While loopYesYesVariable step size or early exit
Enhanced for loopNo (primitives)NoRead-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.
Write the loop condition and body to find the index of the maximum value in an int array named scores.
4.6

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.
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 classPrimitiveParse methodImmutable?
IntegerintInteger.parseInt(String)Yes
DoubledoubleDouble.parseDouble(String)Yes
4.8

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.
An ArrayList<Integer> contains [5, 3, 8, 3, 2]. Write the loop that removes all elements equal to 3 without skipping any element.
MethodReturnsEffect on size
size()intNone
add(E)voidIncreases by 1
add(int, E)voidIncreases by 1, shifts right
remove(int)E (removed element)Decreases by 1, shifts left
set(int, E)E (previous element)None
4.11

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.
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 orderOuter loop variableInner loop variableTypical use
Row-majorrow index icolumn index jProcess each row left to right
Column-majorcolumn index jrow index iProcess 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.
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.
Trace selection sort on [5, 2, 8, 1, 4]. What does the array look like after two complete passes?
AlgorithmKey operationSwaps per passBest case
Selection sortFind min, swap into positionExactly 1O(n^2) comparisons
Insertion sortShift elements, insert key0 to manyO(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.
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.
Trace binary search for the value 14 in [2, 5, 8, 11, 14, 17, 20]. List the mid value checked at each step.
AlgorithmPreconditionStrategyTime complexity
Linear searchNoneCheck each element in orderO(n)
Binary searchSorted dataEliminate half each stepO(log n)
Selection sortNoneSwap min into place each passO(n^2)
Insertion sortNoneInsert each element into sorted prefixO(n^2) worst, O(n) best
Merge sortNoneSplit then merge recursivelyO(n log n)

Practice AP Computer Science A unit 4 questions

Try AP-style multiple-choice questions and written prompts after you review the notes.

Example AP-style MCQs

open all practice
MCQ

AP-style practice question

Question

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?

The list data must be instantiated and have some elements.

The list data must be instantiated but can remain empty.

The list data must be declared but remain uninstantiated.

The list data must be instantiated and have null elements.

MCQ

AP-style practice question

Question

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?

private int[] espressoSalesClassical; private int[] espressoSalesPopMusic;

private int[] pastriesSalesClassical; private int[] pastriesSalesPopMusic;

private int[] espressoSalesMornings; private int[] espressoSalesAfternoon;

private int[] pastriesSalesMornings; private int[] pastriesSalesAfternoon;

Example FRQs

open all FRQs
FRQ

2D array traversal, row weight calculation

4. The Warehouse class is used to manage the storage of boxes in a grid layout. A partial declaration of the Box class is shown below.

</>Java
public class Box
{
    /**
     * Returns the category of the box (e.g., "electronics", "clothing")
     */
    public String getCategory()
    { /* implementation not shown */ }

    /**
     * Returns the weight of the box in kilograms
     */
    public double getWeight()
    { /* implementation not shown */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
</>Java
public class Warehouse
{
    private Box[][] storage;

    /**
     * Returns the index of the row containing the maximum total weight
     * of boxes belonging to the specified category.
     * Returns -1 if no boxes of the specified category exist in storage.
     * Preconditions: storage is not null and no elements
     *                of storage are null.
     *                storage has at least one row and at
     *                least one column.
     */
    public int rowWithMaxWeight(String category)
    { /* to be implemented */}

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}

When an element of the two-dimensional array storage is accessed, the first index is used to specify the row and the second index is used to specify the column.

Write the Warehouse method rowWithMaxWeight. The method should traverse the 2D array storage to calculate the total weight of boxes matching the parameter category for each row. It should return the index of the row with the highest total weight for that category. If there are multiple rows with the same maximum total weight, return the smallest of those row indices. If no boxes of the specified category are found in the entire array, the method should return -1.

Suppose storage has the following contents. For each element, the first value is the category and the second value is the weight.

Row

0

1

2

0

"parts" 10.0

"toys" 5.0

"parts" 5.0

1

"books" 2.0

"books" 3.0

"books" 1.0

2

"parts" 20.0

"toys" 2.0

"parts" 2.0

3

"toys" 10.0

"toys" 10.0

"parts" 1.0

  • Call: rowWithMaxWeight("parts")

  • Expected Return: should return 2

  • Explanation: Row 0 total: 15.0. Row 1 total: 0.0. Row 2 total: 22.0. Row 3 total: 1.0. The maximum is 22.0 at index 2.

  • Call: rowWithMaxWeight("toys")

  • Expected Return: should return 3

  • Explanation: Row 0 total: 5.0. Row 1 total: 0.0. Row 2 total: 2.0. Row 3 total: 20.0. The maximum is 20.0 at index 3.

  • Call: rowWithMaxWeight("clothing")

  • Expected Return: should return -1

  • Explanation: No boxes with category "clothing" exist in the array.

Complete method rowWithMaxWeight.

</>Java
/**
 * Returns the index of the row containing the maximum total weight
 * of boxes belonging to the specified category.
 * Returns -1 if no boxes of the specified category exist in storage.
 * Preconditions: storage is not null and no elements
 *                of storage are null.
 *                storage has at least one row and at
 *                least one column.
 */
public int rowWithMaxWeight(String category)
FRQ

Temperature averaging with conditional filtering criteria

3. The DailyReading class represents weather data collected on a single day. A partial declaration of the DailyReading class is shown below.

</>Java
public class DailyReading
{
    /**
     * Returns the temperature in degrees Fahrenheit
     */
    public double getTemperature()
    { /* implementation not shown */ }

    /**
     * Returns the wind speed in miles per hour
     */
    public double getWindSpeed()
    { /* implementation not shown */ }

    /**
     * Returns true if rain was recorded, false otherwise
     */
    public boolean isRaining()
    { /* implementation not shown */ }

    /* There may be instance variables, constructors, and
       methods that are not shown. */
}
</>Java
public class WeatherStation
{
    /** The list of daily weather readings */
    private ArrayList<DailyReading> readings;

    /**
     * Returns the average temperature of readings that meet the specified criteria.
     * Precondition: minWind >= 0.0
     * Postcondition: Returns the average temperature of readings where:
     *   - The wind speed is greater than or equal to minWind
     *   - AND (requireRain is false OR isRaining() is true)
     *   Returns 0.0 if no readings meet the criteria.
     */
    public double averageTempWithCondition(boolean requireRain, double minWind)
    { /* to be implemented */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}

Write the WeatherStation method averageTempWithCondition. This method calculates and returns the average temperature of the valid readings in the readings list. A reading is considered valid if it meets the wind speed requirement (at least minWind) and the rain requirement (if requireRain is true, the reading must be raining; if requireRain is false, the rain status does not matter).

Suppose readings contains the following five DailyReading objects.

Index

0

1

2

3

4

Temperature

70.0

80.0

60.0

90.0

65.0

Wind Speed

10.0

5.0

15.0

20.0

12.0

Is Raining

true

false

true

false

true

  • Call: averageTempWithCondition(true, 11.0)

  • Return Value: 62.5

  • Explanation: The method considers only readings where isRaining() is true AND wind speed is >= 11.0. Index 0 fails the wind condition (10.0 < 11.0). Index 1 and 3 fail the rain condition. Index 2 (Wind 15.0, Rain true) and Index 4 (Wind 12.0, Rain true) are valid. The average of their temperatures (60.0 and 65.0) is 62.5.

Complete method averageTempWithCondition.

</>Java
/**
 * Returns the average temperature of readings that meet the specified criteria.
 * Precondition: minWind >= 0.0
 * Postcondition: Returns the average temperature of readings where:
 *   - The wind speed is greater than or equal to minWind
 *   - AND (requireRain is false OR isRaining() is true)
 *   Returns 0.0 if no readings meet the criteria.
 */
public double averageTempWithCondition(boolean requireRain, double minWind)

Key terms

TermDefinition
TraversalThe 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 referenceA 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 typeA 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 errorAn 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.
fileStorage 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 indexThe 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 indexThe 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() methodAn ArrayList method that retrieves the element at a specified index. Syntax: list.get(i) returns the element at index i without removing it.
maximum trackingA 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 algorithmA 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 elementsAn 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 arrayAn 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 unit 4 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.

How this unit shows up on the AP exam

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 unit 4 review checklist

  • Final Unit 4 review checklist: ArraysCreate 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: ArrayListsDeclare 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 ArraysCreate 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 WrappersWrite 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 SortingTrace 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 SortIdentify 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 EthicsExplain 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.

How to study unit 4

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

Open the individual guides for Unit 4 when you want a closer review of one topic.

browse guides

FRQ practice

Practice free-response reasoning and compare your answer with scoring guidance.

practice FRQs

Cheatsheets

Use unit cheatsheets for a quick visual review after you work through the notes.

open cheatsheets

Score calculator

Estimate your broader AP score goal after you review the course and exam format.

open calculator

Frequently Asked Questions

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.

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.

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.

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, 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.

Ready to review Unit 4?Start with the notes, check the topic cards, and use the practice or resource links when they are available for this course.