AP CSA free-response questions: how to answer them
The free-response section of AP Computer Science A asks you to write Java code from a description. Here is how to approach it and earn every mark you can.
On this page
- What the questions ask
- A step-by-step method
- How to earn partial credit
- Common mistakes
- How to practise
- Worked example: a method with a loop
- Worked example: designing a class
- Worked example: an ArrayList
- Worked example: a 2D array
- Practice questions
- Frequently asked questions
- Key takeaways
- Where to go next
- Planning before you code
- Reading the given code
- Testing on paper
- Time management
- More practice
- Common scoring pitfalls
- More questions students ask
- Quick check
- Study tip
- A final word
What the questions ask
Free-response questions typically ask you to:
- Write methods using conditions and loops.
- Design or complete a class with fields, constructors and methods.
- Process data stored in an array or an ArrayList.
- Work with a two-dimensional array.
The exact format can change with course revisions, so check the latest exam description and past questions on AP Central.
A step-by-step method
- Read the whole question, including the class definitions and examples.
- Note the method header: its name, parameters and return type.
- Check preconditions and postconditions. They tell you what you can assume and what you must guarantee.
- Plan in plain English, then write the code.
- Use methods you are given rather than rewriting their logic.
- Trace your code with the example in the question.
How to earn partial credit
- Scoring is based on specific points, so a mostly correct answer still earns marks.
- Write something for every part, even if you are unsure.
- If a later part depends on an earlier method, call that method as if it works.
Common mistakes
- Off-by-one errors in loop bounds.
- Removing items from an ArrayList while looping forward through it.
- Comparing strings with == instead of equals.
- Forgetting to return a value.
How to practise
- Do past free-response questions on paper under time limits.
- Mark them with the official scoring guidelines.
- Rewrite each answer after marking, fixing every lost point.
Worked example: a method with a loop
Task: write a method that returns how many even numbers are in an array.
public static int countEvens(int[] nums) {
int count = 0;
for (int n : nums) {
if (n % 2 == 0) {
count++;
}
}
return count;
}
Points usually come from: looping over every element, testing the condition correctly, updating the count, and returning the result.
Worked example: designing a class
Task: write a BankAccount class with a balance, a constructor, and methods to deposit and to withdraw only if there is enough money.
public class BankAccount {
private double balance;
public BankAccount(double initial) {
balance = initial;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public double getBalance() {
return balance;
}
}
Notice the private field, the constructor that sets it, and methods with the exact names and return types the question asks for.
Worked example: an ArrayList
Task: remove every word shorter than 4 letters from an ArrayList<String>.
public static void removeShort(ArrayList<String> words) {
for (int i = words.size() - 1; i >= 0; i--) {
if (words.get(i).length() < 4) {
words.remove(i);
}
}
}
Looping backwards avoids skipping elements when items shift left after a removal.
Worked example: a 2D array
Task: return the sum of one column of a 2D array.
public static int columnSum(int[][] grid, int col) {
int total = 0;
for (int r = 0; r < grid.length; r++) {
total += grid[r][col];
}
return total;
}
Rows come first: grid[r][c]. grid.length is the number of rows.
Practice questions
Try each one before opening the answer.
Q1. Write the loop header to visit every element of int[] arr by index.
Show answer
for (int i = 0; i < arr.length; i++).Q2. Why should you compare strings with equals and not ==?
Show answer
== compares whether two references point to the same object; equals compares the characters.Q3. How do you get the number of columns in a rectangular 2D array grid?
Show answer
grid[0].length.Frequently asked questions
Do I lose marks for small syntax errors?
Minor syntax slips are often not penalised if the logic is clear, but check the current scoring guidelines. Logic errors do cost points.
Should I write comments?
They are not required. Clear variable names help graders follow your logic.
Key takeaways
- Read the full question, including given classes and examples.
- Match method names, parameters and return types exactly.
- Use given methods instead of rewriting their logic.
- Watch loop bounds, ArrayList removals and string comparison.
- Answer every part; scoring is point by point.
Where to go next
Build the coding habits that make free-response questions easier.
Planning before you code
Strong answers often start with a few seconds of planning. Write two or three short notes in plain English: what to loop over, what condition to test, and what to return. For example: "go through each word; if it has more than four letters, add one to the count; return the count." Then translate each note into a line of Java.
Reading the given code
Questions often give you a class with some methods already written. Read their headers carefully: the names, parameters and return types tell you what tools you have. If a method called isValid is provided, use it rather than writing your own check. Graders expect you to use given methods, and doing so saves time.
Testing on paper
After writing, trace your code with the example in the question. Write the value of each variable after every loop. This catches most off-by-one errors and missed cases, such as an empty array or a value at the very end.
Time management
Divide the free-response time roughly equally between the questions. If you are stuck on one part, write what you can, move on, and return later. A partly correct answer earns more than a blank page.
More practice
Try each one before opening the answer.
Q1. Write an expression that is true when an int n is between 1 and 10 inclusive.
Show answer
n >= 1 && n <= 10.Common scoring pitfalls
- Changing the method header. If the question gives the header, copy it exactly.
- Printing instead of returning. If the method has a return type, return the value; do not print it.
- Accessing out-of-range indices. Check that loops stop at length − 1 and that i + 1 stays in range when comparing neighbours.
- Modifying data you were told not to change. Read whether the method should change the array or leave it unchanged.
More questions students ask
Can I use Java features not taught in the course?
Usually yes, if they work correctly, but sticking to the course subset is safest and avoids surprises.
Quick check
A method should return the average of an int[]. Why might sum / arr.length give the wrong answer?
Show answer
(double) sum / arr.length.Study tip
Keep a notebook of every FRQ bug you make, with the fix. Read it before each practice session.
A final word
Free-response questions reward clear, careful code more than clever code. Plan in plain English, use the methods you are given, trace your solution with the example, and answer every part. Steady practice with past questions builds the confidence to do this under time pressure.