Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 20: Capstone — A Full FRQ Set, Designed and Defended

This final lesson brings the whole course together in the form you will meet on the day: a set of free-response questions, each drawing on several units at once, and each demanding working code written to an exact specification. The questions in the capstone are presented as worked examples, and your job is not to memorize their answers but to study how a complete response is built: read the specification, choose the right data structure and algorithm, write the method, and then trace it on a small example to check it. That routine is the same whether the question is about arrays, inheritance, or recursion.

Start every question by naming what it asks for: the class or method to write, its parameters, its return type, and its behaviour. A question that asks for a method operating on a two-dimensional array calls for nested loops with the right bounds; a question that asks for a subclass calls for extends, a super call, and an override; a question that asks for a search calls for a loop with an early return or a −1 at the end. If you choose the wrong structure, the code that follows cannot recover, so spend the first minute of each question planning rather than writing. A short comment or a sketch of the algorithm is time well spent.

The middle of an answer is where the rubric's points live, and they are won with correct details. Use the loop bound that matches the data structure — length for arrays, size() for ArrayLists, grid[row].length for a row of a grid — and test the condition you actually need, not a similar one. Check every method call against the Quick Reference: the name, the parameter types, and whether it returns a value. And remember the recurring traps: integer division where a double is needed, == on Strings, removing from an ArrayList in a forward loop, and a base case that never stops a recursion.

The end of an answer is a check, not a formality. Trace the method on the smallest input that exercises it: a one-element array, a list with the target at the end, a subclass object stored in a superclass variable. If the trace works, the method is probably right; if it does not, the trace shows exactly where it fails, and you can fix that line rather than rewriting the whole answer. A method that has been traced on a small example is worth more than a longer method that has never been run, even in your head.

Before you hand in a free-response answer, read it as the compiler would: check that every statement ends with a semicolon, every opening brace has a closing brace, every variable is declared before it is used, and every method returns a value of the right type on every path. The commonest compile errors in handwritten code are a missing semicolon after a return statement, a method call with the wrong number of arguments, and a variable used outside the block where it was declared, such as a loop variable used after its loop. A minute spent on this check can save the points that a single missing character would otherwise cost, because code that does not compile cannot earn the points for the behaviour it was meant to have.

A worked example

Worked example: a worked-example free-response question asks for a method that returns the row index of the largest value in a two-dimensional array, assuming the array has at least one element. A complete answer initializes the best row to 0 and the best value to the first element, then traverses the whole grid, updating both whenever a larger value is found, and returns the best row after the loops.

public int rowOfLargest(int[][] grid)
{
    int bestRow = 0;
    int bestValue = grid[0][0];
    for (int row = 0; row < grid.length; row++)
    {
        for (int col = 0; col < grid[row].length; col++)
        {
            if (grid[row][col] > bestValue)
            {
                bestValue = grid[row][col];
                bestRow = row;
            }
        }
    }
    return bestRow;
}
// rowOfLargest({{1, 9}, {4, 3}}) returns 0, since 9 is in row 0

The method starts with the first element as the current best, so it works even when all values are negative, and it updates both the best value and the best row together, so they never disagree. The nested loops use grid.length and grid[row].length as their bounds, and the return sits after both loops. Tracing on the example: the grid's largest value is 9 in row 0, so the method returns 0. The capstone habit to take away is a routine: plan, write, trace — and by the time you reach the timed mock test, that routine will be automatic.

Key Terms

Capstone
A final set of questions drawing on the whole course at once.
Specification
The exact method header and behaviour a question asks for.
Trace
Following code by hand on a small input to check it works.
Routine
A repeatable working method: plan, write, then trace and check.

Discussion Questions

  1. Why should you spend the first minute of a question planning rather than writing?
  2. What is the smallest input that would test a method on a two-dimensional array?
  3. How does tracing on a small example catch errors a re-read might miss?

Practice Questions

  1. A question asks for a method on an int[] that returns a boolean. What must the method do on every path?
  2. Why initialize a maximum search with the first element rather than 0?
  3. A method on a 2D array uses grid.length for both loop bounds. What is wrong?
  4. What is the first thing to check after writing a free-response method?
  5. Name the three steps of the capstone routine.
Show answers
  1. Return a boolean value, true or false, on every possible path through the method.
  2. So it works when all the values are negative.
  3. The inner bound should be grid[row].length, the number of columns, not the number of rows.
  4. Trace it on a small input to confirm it behaves as specified.
  5. Plan, write, then trace and check.
Keep reading to complete this lesson