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
| Concept | Syntax | Notes |
|---|---|---|
| Variable declaration | int 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 division | 7 / 2 = 3 (not 3.5) | Key trap: int/int = int (truncates). Cast to double: (double) 7 / 2 = 3.5 |
| String methods | s.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 class | Math.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) |
| Casting | int x = (int) 3.9; // x = 3 | Casting truncates (not rounds). (int) 3.9 = 3, not 4. |
| null | String s = null; | Calling a method on null throws NullPointerException. Always check before using object methods. |
🏗️ OOP — Classes, Inheritance & Interfaces
| Concept | Key rule | AP exam tip |
|---|---|---|
| Class definition | Instance 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 overriding | Subclass 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 classes | Cannot 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). |
| Polymorphism | A 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. |
instanceof | Tests 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
| Array | ArrayList | |
|---|---|---|
| Declaration | int[] arr = new int[5]; | ArrayList<Integer> list = new ArrayList<>(); |
| Access | arr[i] (0-indexed) | list.get(i) |
| Size | arr.length (no parentheses) | list.size() (with parentheses) |
| Add element | Not possible (fixed size) | list.add(x) or list.add(i, x) |
| Remove | Not possible (only overwrite) | list.remove(i) (by index) or list.remove(obj) (by object) |
| Key traps | Off-by-one errors; default values: 0 for int, null for objects | Removing 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
| Pattern | Code | When to use |
|---|---|---|
| Standard for loop | for (int i = 0; i < arr.length; i++) | When you need the index, or when modifying elements |
| Enhanced for loop | for (int x : arr) | When you only need values, not index. Cannot modify array elements via enhanced loop (modifying x doesn't change arr). |
| While loop | while (condition) { ... } | When number of iterations unknown in advance. Check for infinite loop condition on exam. |
| ArrayList traversal — remove safely | for (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
| Concept | Key rule |
|---|---|
| Base case | The 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 call | Must move toward the base case with each call. If it doesn't, infinite recursion occurs. |
| Tracing recursion | Write out the call stack. factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6. Work from innermost outward. |
| AP common recursive patterns | Factorial, 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
| Algorithm | Best case | Worst case | Key property |
|---|---|---|---|
| Sequential (linear) search | O(1) — first element | O(n) | Works on unsorted arrays. Check every element. |
| Binary search | O(1) — midpoint | O(log n) | Requires SORTED array. Halves search space each iteration. |
| Selection sort | O(n²) | O(n²) | Find minimum, swap to front; repeat for remaining. Always O(n²) regardless of input. |
| Insertion sort | O(n) — already sorted | O(n²) | Build sorted portion by inserting each element. Best for nearly-sorted data. |
| Merge sort | O(n log n) | O(n log n) | Divide and conquer; always O(n log n). Uses extra space (not in-place). |