STEM-TutorLearn • Practice • ExcelBook Free DemoFree demo

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
  1. What the questions ask
  2. A step-by-step method
  3. How to earn partial credit
  4. Common mistakes
  5. How to practise
  6. Worked example: a method with a loop
  7. Worked example: designing a class
  8. Worked example: an ArrayList
  9. Worked example: a 2D array
  10. Practice questions
  11. Frequently asked questions
  12. Key takeaways
  13. Where to go next
  14. Planning before you code
  15. Reading the given code
  16. Testing on paper
  17. Time management
  18. More practice
  19. Common scoring pitfalls
  20. More questions students ask
  21. Quick check
  22. Study tip
  23. A final word

What the questions ask

Free-response questions typically ask you to:

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

  1. Read the whole question, including the class definitions and examples.
  2. Note the method header: its name, parameters and return type.
  3. Check preconditions and postconditions. They tell you what you can assume and what you must guarantee.
  4. Plan in plain English, then write the code.
  5. Use methods you are given rather than rewriting their logic.
  6. Trace your code with the example in the question.

How to earn partial credit

Common mistakes

How to practise

  1. Do past free-response questions on paper under time limits.
  2. Mark them with the official scoring guidelines.
  3. 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

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

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
Both are ints, so integer division discards the decimal part. Cast one to double: (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.