AP CSA free-response questions: the four types, with worked Java examples
9 min readUpdated
The AP Computer Science A free-response section asks you to write Java by hand. The questions change every year, but their shapes do not. Below is one short worked example of each shape, written the way examiners like to see it: simple, correct and easy to follow.
1. Methods and control structures
You write one or two methods using loops, conditions and String methods. A typical task: count how many times a short string appears inside a longer one.
public class CountOccurrences {
public static int countOf(String text, String part) {
int count = 0;
for (int i = 0; i <= text.length() - part.length(); i++) {
if (text.substring(i, i + part.length()).equals(part)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(countOf("banana", "an"));
System.out.println(countOf("aaaa", "aa"));
System.out.println(countOf("java", "z"));
}
}
Output
2 3 0
2. Class design
You write a complete class from a description: private instance variables, a constructor and the methods asked for. Keep fields private and update them only through methods.
public class StepTracker {
private int activeGoal;
private int activeDays;
private int days;
private int totalSteps;
public StepTracker(int activeGoal) {
this.activeGoal = activeGoal;
}
public void addDailySteps(int steps) {
days++;
totalSteps += steps;
if (steps >= activeGoal) {
activeDays++;
}
}
public int activeDays() {
return activeDays;
}
public double averageSteps() {
if (days == 0) {
return 0.0;
}
return (double) totalSteps / days;
}
public static void main(String[] args) {
StepTracker tracker = new StepTracker(10000);
tracker.addDailySteps(9000);
tracker.addDailySteps(12000);
tracker.addDailySteps(10000);
System.out.println(tracker.activeDays());
System.out.println(tracker.averageSteps());
}
}
Output
2 10333.333333333334
3. Data analysis with an ArrayList
You search, filter or change an ArrayList. When you remove items while looping, loop backwards so no element is skipped.
import java.util.ArrayList;
import java.util.List;
public class RemoveShortWords {
public static void removeShort(ArrayList<String> words, int minLength) {
for (int i = words.size() - 1; i >= 0; i--) {
if (words.get(i).length() < minLength) {
words.remove(i);
}
}
}
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>(List.of("a", "tree", "is", "tall", "go"));
removeShort(words, 3);
System.out.println(words);
}
}
Output
[tree, tall]
4. 2D arrays
You traverse a 2D array by rows or by columns. For column work, the outer loop runs over columns and the inner loop over rows.
import java.util.Arrays;
public class ColumnTotals {
public static int[] columnTotals(int[][] grid) {
int[] totals = new int[grid[0].length];
for (int col = 0; col < grid[0].length; col++) {
for (int row = 0; row < grid.length; row++) {
totals[col] += grid[row][col];
}
}
return totals;
}
public static void main(String[] args) {
int[][] grid = { {1, 2, 3}, {4, 5, 6} };
System.out.println(Arrays.toString(columnTotals(grid)));
}
}
Output
[5, 7, 9]
How to write answers that score well
- Read the method header you are given and keep its exact name, parameters and return type.
- Use methods from earlier parts of the question when the question says you may; it saves time and marks.
- Write clear, simple loops. Examiners mark correctness, not cleverness.
- Return a value on every path, and check the empty or single-item case.
- Practise against past questions on AP Central and mark them with the official scoring guidelines.
Frequently asked questions
How many free-response questions are on the AP CSA exam?
Four, one of each type: methods and control structures, class design, data analysis with an ArrayList, and a 2D array question. Check College Board's current exam description for timing.
Do small syntax errors lose marks on AP CSA free-response?
Minor slips such as a missing semicolon are usually not penalised, but logic errors, wrong method calls and missing return values are. Focus on correct logic.
How long should I spend on each free-response question?
Roughly the same time on each, about 20 to 22 minutes, leaving a few minutes to check your answers.
Learn it step by step
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.