AP Computer Science A Reference Sheet

AP Computer Science A Reference Sheet

Complete Java Quick Reference for the 2025-2026 AP CSA Exam

📥 Want a Print-Ready PDF Bundle?

Get the premium bundle: 2-page condensed cheat sheet, FRQ scoring patterns, 10 code templates, 15 practice questions with answers, and exam day strategy guide.

Get PDF Bundle — $2.99

String Methods

Method Returns Example
length() int "Hello".length() → 5
substring(start, end) String "Hello".substring(1, 4) → "ell"
substring(start) String "Hello".substring(2) → "llo"
indexOf(str) int "Hello".indexOf("l") → 2
equals(str) boolean "Hi".equals("hi") → false
compareTo(str) int "A".compareTo("B") → negative
charAt(index) char "Hello".charAt(0) → 'H'
💡 Remember: String indices start at 0. substring(start, end) includes start but excludes end.

Math Methods

Method Returns Example
Math.abs(x) int/double Math.abs(-5) → 5
Math.pow(base, exp) double Math.pow(2, 3) → 8.0
Math.sqrt(x) double Math.sqrt(16) → 4.0
Math.random() double Returns 0.0 ≤ x < 1.0
💡 Random Range Formula: (int)(Math.random() * range) + min
Example: Random 1-6 (dice roll): (int)(Math.random() * 6) + 1

ArrayList Methods

Method Returns Description
size() int Number of elements
get(index) E Element at index
set(index, value) E Replace element, returns old value
add(value) boolean Add to end
add(index, value) void Insert at index, shifts others right
remove(index) E Remove at index, shifts others left

Arrays vs ArrayList

Operation Array ArrayList
Declare int[] arr = new int[5]; ArrayList list = new ArrayList<>();
Size arr.length list.size()
Access arr[i] list.get(i)
Modify arr[i] = value; list.set(i, value);
Add element N/A (fixed size) list.add(value);

2D Array Basics

Operation Code
Declare int[][] grid = new int[rows][cols];
Number of rows grid.length
Number of columns grid[0].length
Access element grid[row][col]
Get entire row grid[row] (returns 1D array)

2D Array Traversal

for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length; col++) { // process grid[row][col] } }

Operators

Type Operators
Arithmetic + - * / % (mod = remainder)
Comparison == != < > <= >=
Logical && (and) || (or) ! (not)
Assignment = += -= *= /= %=
Increment/Decrement x++ ++x x-- --x

Boolean Operators (Truth Table)

A B A && B A || B !A
true true true true false
true false false true false
false true false true true
false false false false true
💡 Memory Trick: AND (&&) requires BOTH true. OR (||) requires at least ONE true.

compareTo() Return Values

Expression Returns Meaning
a.compareTo(b) negative (e.g., -1) a comes BEFORE b alphabetically
a.compareTo(b) 0 a EQUALS b
a.compareTo(b) positive (e.g., 1) a comes AFTER b alphabetically
💡 Example: "Apple".compareTo("Banana") → negative (A comes before B)

Wrapper Classes (Autoboxing)

Primitive Wrapper Why You Need It
int Integer ArrayList — can't use primitives in generics
double Double Autoboxing: Integer x = 5; (auto-converts)
boolean Boolean Unboxing: int y = x; (auto-converts back)

Loop Syntax

Type Syntax
for loop for (int i = 0; i < n; i++) { }
while loop while (condition) { }
for-each loop for (Type item : collection) { }
💡 For-each limitation: Cannot modify the collection or access the index. Use a regular for loop if you need either.

Selection Statements

Type Syntax
if if (condition) { }
if-else if (condition) { } else { }
if-else if if (c1) { } else if (c2) { } else { }

Class Structure (FRQ 2)

public class ClassName { private int instanceVar; // instance variable public ClassName(int param) { // constructor instanceVar = param; } public int getInstanceVar() { // accessor (getter) return instanceVar; } public void setInstanceVar(int v) { // mutator (setter) instanceVar = v; } }

Common Traps (Watch Out!)

Trap Problem Fix
= vs == if (x = 5) assigns, not compares Use == for comparison
Integer division 5/2 = 2, not 2.5 Cast: (double)5/2
String == Compares references, not content Use .equals()
Off-by-one < vs <=, wrong start index Trace with examples
Null pointer Calling method on null Check != null first
; after if/while if (x > 0); { } — empty body! Remove the semicolon
ArrayList forward delete Skips elements when removing Loop backward

Essential Code Patterns

Accumulator (Sum)

int total = 0; for (int i = 0; i < arr.length; i++) { total += arr[i]; } return total;

Find Maximum

int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max;

ArrayList Removal (Loop Backward!)

for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i).equals(target)) { list.remove(i); } }

Linear Search

for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; } } return -1; // not found

📚 Ready to Ace the AP CSA Exam?

Get the complete Premium PDF Bundle with FRQ scoring patterns, 10 code templates, 15 practice questions, and exam day strategy.

Download PDF Bundle — $2.99

© 2025 apcsexamprep.com — Good luck on your exam!

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com