AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/4)
Unit 4
30–40% of examData Collections
Unit 4 is the biggest unit on the exam. You store many values together in arrays, ArrayLists and 2D arrays, read data from text files, and write the standard algorithms that search, count, total and rearrange that data. You also trace searching and sorting algorithms and recursive methods, and think about privacy and bias in the data you collect. Two of the four free-response questions come from this unit.
Study this unit
Flashcards (40)Practice questions (83)Computer Science A must-know sheetFree-response questions on this unit
Write your own answer, then score it with the rubric or with AI.
- Question 1: Methods and Control StructuresRewards card constructor and top item7 points · about 25 minutes
- Question 2: Class DesignParty guest list7 points · about 25 minutes
- Question 3: Data Analysis with ArrayListRemoving short songs from a playlist5 points · about 18 minutes
- Question 3: Data Analysis with ArrayListVolunteers who reach an hours goal5 points · about 18 minutes
- Question 3: Data Analysis with ArrayListKeeping a to-do list in priority order5 points · about 18 minutes
- Question 3: Data Analysis with ArrayListBiggest day-to-day warm-up5 points · about 18 minutes
- Question 3: Data Analysis with ArrayListMerging duplicate inventory entries5 points · about 18 minutes
- Question 4: 2D ArrayFinding a block of empty seats6 points · about 22 minutes
- Question 4: 2D ArrayCounting peaks on a height map6 points · about 22 minutes
- Question 4: 2D ArrayFilling a letter grid from a String6 points · about 22 minutes
- Question 4: 2D ArrayCrop totals for each column of a garden6 points · about 22 minutes
- Question 4: 2D ArrayMirroring a picture left to right6 points · about 22 minutes
Big ideas
- An array has a fixed size, while an
ArrayListcan grow and shrink - Traversing a collection with a loop is the base of almost every algorithm here
- Removing items while traversing an
ArrayListneeds care to avoid skipping elements - A 2D array is an array of rows, usually traversed in row-major order
- Binary search and merge sort repeatedly cut a problem in half
- Data can be biased or incomplete, and collecting it puts people's privacy at risk
Full unit reviews
Longer videos that cover the whole unit. Good for a first pass or a final review.
Topics
- 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
Collecting and storing personal data puts people's privacy at risk, so programmers should try to protect it. Data can be biased, incomplete or inaccurate, and algorithmic bias is when a program's repeated errors create unfair results for certain groups. A data set gathered for one question may not be the right one for answering a different question.
Key terms
- privacy
- personal data
- algorithmic bias
- data quality
A few quick questions on this topic, with the answers explained.
New in the 2025 course. A data set is a collection of related pieces of information that a program can analyze to answer a question. Programs usually look at the values one at a time and process each one, and sketching the data in a table or chart first helps you plan the algorithm.
Key terms
- data set
- processing data one value at a time
- table
A few quick questions on this topic, with the answers explained.
An array stores a fixed number of values of the same type, and its size can't change once it's created. You create one with new int[5] (elements start at default values like 0, false or null) or with an initializer list like {3, 1, 4}. Valid indexes run from 0 to arr.length - 1; anything else throws an ArrayIndexOutOfBoundsException.
Key terms
- array
- element
- index
length- initializer list
ArrayIndexOutOfBoundsException
A few quick questions on this topic, with the answers explained.
Traversing an array means using a loop to visit its elements, either by index with a for or while loop or with an enhanced for loop (for (int x : arr)). The enhanced for variable is a copy of each element, so assigning to it doesn't change the array, though calling methods on it can change the objects the array refers to.
Key terms
- traversal
- enhanced
forloop - indexed loop
- loop variable copy
A few quick questions on this topic, with the answers explained.
Standard array algorithms include finding a minimum or maximum, computing a sum or average, checking whether at least one or all elements have a property, counting matches, comparing consecutive pairs, detecting duplicates, shifting or rotating elements, and reversing the array. Most are a single traversal with a well-chosen variable to track the answer.
Key terms
- min and max
- sum and average
- count
- consecutive pairs
- duplicates
- shift, rotate and reverse
A few quick questions on this topic, with the answers explained.
New in the 2025 course. A text file keeps data after the program stops running. You open one with new Scanner(new File("data.txt")), add throws IOException to the method header, and import File and IOException from java.io and Scanner from java.util. You read with methods like nextInt, next and nextLine, loop with while (input.hasNext()), call close() when you're done, and use split to break a line into a String array.
Key terms
FileScannerthrows IOExceptionhasNextnextLinesplit
A few quick questions on this topic, with the answers explained.
Integer and Double are wrapper classes that turn primitive values into immutable objects, which matters because an ArrayList can only hold objects. Java converts automatically: autoboxing turns an int into an Integer, and unboxing turns it back. Integer.parseInt and Double.parseDouble turn a String into a number.
Key terms
- wrapper class
IntegerDouble- autoboxing
- unboxing
Integer.parseInt
A few quick questions on this topic, with the answers explained.
An ArrayList<E> is a resizable list of object references from java.util. Its key methods are size(), add(obj), add(index, obj), get(index), set(index, obj) and remove(index); adding or removing in the middle shifts the later elements and changes their indexes.
Key terms
ArrayList- generic type
<E> sizeaddgetandsetremove
A few quick questions on this topic, with the answers explained.
You traverse an ArrayList with an indexed loop using get(i) and size(), or with an enhanced for loop. Removing elements while looping forward can skip the next element, so adjust the index or loop backward, and never add or remove inside an enhanced for loop, which can throw a ConcurrentModificationException.
Key terms
ArrayListtraversal- skipped elements
IndexOutOfBoundsExceptionConcurrentModificationException
A few quick questions on this topic, with the answers explained.
The array algorithms all work on an ArrayList too, and the list's methods make inserting and deleting elements much easier. Some problems need you to traverse two collections at once, such as comparing two lists or building one list from another.
Key terms
- insert and delete
- parallel traversal
- filtering a list
- standard algorithms
A few quick questions on this topic, with the answers explained.
A 2D array is an array of arrays, written like int[][] grid = new int[3][4] for 3 rows and 4 columns. You access an element with grid[row][col]; grid.length is the number of rows and grid[0].length is the number of columns. On the exam every 2D array is rectangular.
Key terms
- 2D array
- row
- column
arr[row][col]- row and column length
A few quick questions on this topic, with the answers explained.
You traverse a 2D array with nested loops. Row-major order goes across each row before moving down, and column-major order goes down each column before moving across. In a nested enhanced for loop, the outer variable is a whole row (a 1D array) and the inner variable is one element.
Key terms
- nested loops
- row-major order
- column-major order
- enhanced
forover rows
A few quick questions on this topic, with the answers explained.
The standard array algorithms carry over to 2D arrays, either across the whole grid or for one row, one column or another section. Typical tasks are totals and averages for each row or column, finding the largest value, counting matches, checking neighbors, and shifting or reversing a row or column.
Key terms
- row and column totals
- subsection
- neighbors
- 2D min and max
A few quick questions on this topic, with the answers explained.
Linear search checks the elements one at a time, from either end, until it finds the target or runs out of elements. It works on unsorted data in arrays and ArrayLists, and in a 2D array you run it on each row in turn.
Key terms
- linear (sequential) search
- target
- return -1 when not found
A few quick questions on this topic, with the answers explained.
Selection sort repeatedly finds the smallest (or largest) remaining element and swaps it into its final position. Insertion sort takes the next element and shifts larger elements in the sorted part over to slide it into place, so its position may still change later. You need to be able to trace each pass of both.
Key terms
- selection sort
- insertion sort
- swap
- shift
- pass
A few quick questions on this topic, with the answers explained.
Recursion
A recursive method calls itself. It needs at least one base case that stops the recursion and a recursive call that moves toward it, and each call gets its own copies of the parameters and local variables. Anything done with recursion can also be done with a loop. On the exam you trace recursive methods to find what they return or print; you won't be asked to write one.
Key terms
- recursion
- base case
- recursive call
- call stack
- tracing
A few quick questions on this topic, with the answers explained.
Recursion can walk through a string, array or ArrayList one piece at a time. Binary search only works on sorted data: it checks the middle element and throws away the half that can't hold the target, so it's usually much faster than linear search. Merge sort recursively splits a list into halves, sorts each half and merges them back together. You need to trace each step of both.
Key terms
- binary search
- sorted data
- merge sort
- divide and conquer
- merge
A few quick questions on this topic, with the answers explained.