AP Computer Science A Cheat Sheet 2026

Java syntax, OOP concepts, arrays, ArrayLists, 2D arrays, recursion, algorithm analysis, and common AP CS A exam traps — printable one-page reference.

☕ Java Basics — Syntax Quick Reference

ConceptSyntaxNotes
Variable declarationint x = 5; double y = 3.14; boolean flag = true; String s = "hi";Java is strongly typed. Primitives: int, double, boolean, char. Objects: String, Integer, etc.
Integer division7 / 2 = 3 (not 3.5)Key trap: int/int = int (truncates). Cast to double: (double) 7 / 2 = 3.5
String methodss.length(), s.substring(i,j), s.indexOf("x"), s.equals("hi")Use .equals() not == for String comparison. substring(i,j) returns chars at index i through j-1.
Math classMath.abs(x), Math.pow(x,n), Math.sqrt(x), Math.random()Math.random() returns [0.0, 1.0). To get random int 0–9: (int)(Math.random() * 10)
Castingint x = (int) 3.9; // x = 3Casting truncates (not rounds). (int) 3.9 = 3, not 4.
nullString s = null;Calling a method on null throws NullPointerException. Always check before using object methods.

🏗️ OOP — Classes, Inheritance & Interfaces

ConceptKey ruleAP exam tip
Class definitionInstance variables (private), constructor, getter/setter methods. Constructor has same name as class, no return type.Private instance variables + public accessors = encapsulation. Never public instance variables on the exam.
Inheritance (extends)Subclass inherits all non-private methods from superclass. Use super() to call parent constructor. Subclass can override methods.super() must be first line in subclass constructor. A subclass IS-A superclass (polymorphism).
Method overridingSubclass defines a method with same signature as a superclass method → replaces it. Call parent version with super.methodName().Overriding ≠ overloading. Overloading = same name, different parameters (same class). Overriding = same signature in subclass.
Abstract classesCannot be instantiated directly. Subclasses must implement abstract methods. Uses keyword abstract.Abstract classes can have non-abstract methods (concrete methods with implementation). Interfaces have only abstract methods (pre-Java 8).
PolymorphismA variable declared as a supertype can hold a subclass object. Method calls resolved at runtime (dynamic dispatch).Animal a = new Dog(); — compiles because Dog IS-A Animal. At runtime, Dog's overridden methods execute.
instanceofTests whether an object is an instance of a class: if (a instanceof Dog)Only needed before downcasting. Required to avoid ClassCastException when casting from supertype to subtype.

📋 Arrays & ArrayLists

ArrayArrayList
Declarationint[] arr = new int[5];ArrayList<Integer> list = new ArrayList<>();
Accessarr[i] (0-indexed)list.get(i)
Sizearr.length (no parentheses)list.size() (with parentheses)
Add elementNot possible (fixed size)list.add(x) or list.add(i, x)
RemoveNot possible (only overwrite)list.remove(i) (by index) or list.remove(obj) (by object)
Key trapsOff-by-one errors; default values: 0 for int, null for objectsRemoving while iterating with index — use backward traversal or iterator
2D arrays: int[][] grid = new int[rows][cols]; Access: grid[r][c]. Traverse: nested for loops. grid.length = number of rows; grid[0].length = number of columns. Row-major order: outer loop over rows.

🔄 Loops & Iteration Patterns

PatternCodeWhen to use
Standard for loopfor (int i = 0; i < arr.length; i++)When you need the index, or when modifying elements
Enhanced for loopfor (int x : arr)When you only need values, not index. Cannot modify array elements via enhanced loop (modifying x doesn't change arr).
While loopwhile (condition) { ... }When number of iterations unknown in advance. Check for infinite loop condition on exam.
ArrayList traversal — remove safelyfor (int i = list.size()-1; i >= 0; i--) { if (...) list.remove(i); }Backward traversal prevents index skipping when removing. Forward removal skips the element after the removed one.

🔁 Recursion Shortcuts

ConceptKey rule
Base caseThe stopping condition that returns without a recursive call. Every recursive method must have at least one base case or it will cause a StackOverflowError.
Recursive callMust move toward the base case with each call. If it doesn't, infinite recursion occurs.
Tracing recursionWrite out the call stack. factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6. Work from innermost outward.
AP common recursive patternsFactorial, Fibonacci, binary search, string reversal, sum of array. Know how to trace these manually.
Example — binary search (recursive): Each call halves the search space. If mid == target, return; if target < arr[mid], search left half; else search right half. Base case: lo > hi → not found. Time complexity: O(log n).

⚡ Algorithm Analysis & Sorting

AlgorithmBest caseWorst caseKey property
Sequential (linear) searchO(1) — first elementO(n)Works on unsorted arrays. Check every element.
Binary searchO(1) — midpointO(log n)Requires SORTED array. Halves search space each iteration.
Selection sortO(n²)O(n²)Find minimum, swap to front; repeat for remaining. Always O(n²) regardless of input.
Insertion sortO(n) — already sortedO(n²)Build sorted portion by inserting each element. Best for nearly-sorted data.
Merge sortO(n log n)O(n log n)Divide and conquer; always O(n log n). Uses extra space (not in-place).
AP CS A Practice Test → AP CS A FRQ Guide → AP CS A Score Calculator →