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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Jump to a section
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:
- Make a variable table. Write each variable's name and its current value. Update the table as you read.
- Go one statement at a time, in order. Never skip ahead or guess. The computer has no memory of intent; nor should you.
- Apply precedence and integer rules. Multiplicative before additive;
int / inttruncates;%gives a remainder. - Record every output exactly. For each
printorprintln, 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)excludesto. - Math:
Math.abs(x),Math.pow(base, exp)(returnsdouble),Math.sqrt(x)(returnsdouble),Math.random()(adoublein[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:
- Read the header and the required result. Note the return type (or
void) and the exact output or value expected. - Plan the steps in order, then declare any variables you need.
- Write the logic using only the Java subset, matching types carefully (an
intwill not hold adoublewithout a cast). - End a non-void method with a
returnof the correct type. - 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 / inttruncates.9 / 4is2. To keep the fraction, cast an operand first:(double) 9 / 4is2.25. - Casting position.
(double) (a / b)truncates before casting;(double) a / bcasts first. Cast before the division. - String indexing. Zero-based, and
substring(from, to)excludesto."COMPUTER".substring(3, 6)is"PUT"(indices 3, 4, 5). equalsversus==. Compare object contents (Strings, wrappers) withequals;==compares references and can give a wrongfalse.
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.
- State the value of
17 / 5and17 % 5. (2 marks) - What does
(double) (7 / 2)evaluate to, and why? (2 marks) - Give the output of
System.out.print("A"); System.out.println("B"); System.out.print("C");. (2 marks) - State the value of
"COMPUTER".substring(2, 5). (2 marks) - Why should you use
equalsrather than==to compare two String variables? (2 marks) - After
int x = 10; x += 5; x /= 4;, what isx? (2 marks) - Write the expression for a random integer from 1 to 6 inclusive using
Math.random(). (2 marks) - What does
Integer.parseInt("42") + 1evaluate to, and what would"42" + 1give instead? (2 marks)
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)