Skip to main content
United StatesComputer Science

AP Computer Science A exam technique: tracing Java code, writing free-response methods, and avoiding the common Java syntax and logic traps

A deep-dive AP Computer Science A exam-technique guide for Units 1 and 2. Shows how to trace Java code by hand for the multiple-choice section, how to plan and write the free-response code-writing questions, and how to avoid the integer-division, casting, String-indexing and equals-versus-double-equals traps that the College Board repeats.

Generated by Claude Opus 4.818 min read1.1-2.9

Reviewed by: AI editorial process; not yet individually human-reviewed

Jump to a section
  1. What the AP CSA exam actually demands
  2. Tracing code by hand
  3. The Java subset method signatures
  4. Writing free-response methods
  5. The traps the College Board repeats
  6. Check your knowledge

What the AP CSA exam actually demands

AP Computer Science A is examined in two ways that need two different skills. Section I (multiple choice) is mostly code tracing: you read a Java segment and determine its output, its result, or where it goes wrong. Section II (free response) is entirely code writing: you implement methods that must compile and produce exactly the required behavior. This guide builds both skills on the foundations of Units 1 and 2, and drills the syntax and logic traps the College Board repeats.

The matching dot-point pages each have their own worked exam questions: why programming? why Java?, expressions and assignment statements, casting and ranges of variables, using String objects and methods, and using the Math class.

Tracing code by hand

Tracing is the single most valuable exam skill. The method is mechanical and reliable:

  1. Make a variable table. Write each variable's name and its current value. Update the table as you read.
  2. Go one statement at a time, in order. Never skip ahead or guess. The computer has no memory of intent; nor should you.
  3. Apply precedence and integer rules. Multiplicative before additive; int / int truncates; % gives a remainder.
  4. Record every output exactly. For each print or println, write the characters printed and whether the line ends.

For example, tracing int x = 5; x += 3; x *= 2; gives x = 5, then 8, then 16. A table prevents the slips that cost marks.

The Java subset method signatures

Section II provides a quick reference sheet, but you should know the AP Java subset cold so you do not waste time. The essentials from Units 1 and 2:

  • String: length(), substring(from), substring(from, to), indexOf(str), equals(other), compareTo(other). Indexing is zero-based; substring(from, to) excludes to.
  • Math: Math.abs(x), Math.pow(base, exp) (returns double), Math.sqrt(x) (returns double), Math.random() (a double in [0.0, 1.0)).
  • Wrappers: Integer.parseInt(str), Double.parseDouble(str), Integer.MIN_VALUE, Integer.MAX_VALUE, plus autoboxing and unboxing.

Writing free-response methods

Section II questions give you a method header and a description; you write the body. A dependable routine:

  1. Read the header and the required result. Note the return type (or void) and the exact output or value expected.
  2. Plan the steps in order, then declare any variables you need.
  3. Write the logic using only the Java subset, matching types carefully (an int will not hold a double without a cast).
  4. End a non-void method with a return of the correct type.
  5. Trace your own code with a sample input to confirm it produces the required result, and check braces and semicolons.

The biggest avoidable losses come from output that is almost right: wrong spacing, wrong capitalization, print where println was needed, or a missing colon in a format. Match the required output exactly.

The traps the College Board repeats

These four traps appear on essentially every exam. Internalise them.

  • Integer division. int / int truncates. 9 / 4 is 2. To keep the fraction, cast an operand first: (double) 9 / 4 is 2.25.
  • Casting position. (double) (a / b) truncates before casting; (double) a / b casts first. Cast before the division.
  • String indexing. Zero-based, and substring(from, to) excludes to. "COMPUTER".substring(3, 6) is "PUT" (indices 3, 4, 5).
  • equals versus ==. Compare object contents (Strings, wrappers) with equals; == compares references and can give a wrong false.

Check your knowledge

A mix of tracing, output and code-reasoning questions covering Units 1 and 2. Attempt them under timed conditions, then check against the solutions.

  1. State the value of 17 / 5 and 17 % 5. (2 marks)
  2. What does (double) (7 / 2) evaluate to, and why? (2 marks)
  3. Give the output of System.out.print("A"); System.out.println("B"); System.out.print("C");. (2 marks)
  4. State the value of "COMPUTER".substring(2, 5). (2 marks)
  5. Why should you use equals rather than == to compare two String variables? (2 marks)
  6. After int x = 10; x += 5; x /= 4;, what is x? (2 marks)
  7. Write the expression for a random integer from 1 to 6 inclusive using Math.random(). (2 marks)
  8. What does Integer.parseInt("42") + 1 evaluate to, and what would "42" + 1 give instead? (2 marks)

Sources & how we know this

  • computer-science
  • ap
  • ap-csa
  • java
  • code-tracing
  • free-response
  • exam-technique
  • integer-division
  • strings