AP Computer Science A uses Java to test problem solving, object-oriented programming, and algorithmic thinking. This cheat sheet gives students a compact reference for the syntax and patterns they use most often on the exam. It is useful for reviewing code structure, common library methods, and rules that often appear in multiple-choice and free-response questions.
Students can use it while practicing until the patterns become automatic.
Key Facts
- A Java class is declared with public class ClassName { ... }, and an object is created with ClassName obj = new ClassName(arguments);.
- A method header follows the pattern accessModifier returnType methodName(parameterList), such as public int getScore().
- Integer division drops the decimal part, so 7 / 2 evaluates to 3, while 7.0 / 2 evaluates to 3.5.
- A for loop often uses for (int i = 0; i < list.length; i++) to visit every array index from 0 through list.length - 1.
- An enhanced for loop uses for (Type item : collection) to read each element, but it should not be used when changing array or ArrayList positions.
- String comparison should use str1.equals(str2), because str1 == str2 checks whether the references are the same object.
- ArrayList<E> uses methods such as add(value), add(index, value), get(index), set(index, value), remove(index), and size().
- In inheritance, a subclass can call the superclass constructor with super(arguments), and an overridden method must have the same name, return type, and parameter list.
Vocabulary
- Class
- A blueprint that defines the data fields and methods used to create objects.
- Object
- An instance of a class that stores its own data and can call methods defined by the class.
- Constructor
- A special method with the same name as the class that initializes a new object.
- Array
- A fixed-size collection that stores values of the same type and is accessed using zero-based indexes.
- ArrayList
- A resizable list from java.util that stores object references and provides methods for adding, removing, and accessing elements.
- Polymorphism
- The ability for a superclass reference to refer to a subclass object and call the overridden method at runtime.
Common Mistakes to Avoid
- Using == to compare String values is wrong because == checks object references, not text content. Use str1.equals(str2) when comparing strings.
- Looping with i <= arr.length is wrong because the last valid array index is arr.length - 1. Use i < arr.length to avoid an IndexOutOfBoundsException.
- Removing items from an ArrayList while looping forward can skip elements because later items shift left after a removal. Loop backward or adjust the index after removing.
- Forgetting to initialize an object before calling a method causes a NullPointerException because the reference does not point to an actual object. Create the object with new before using it.
- Assuming integer division rounds normally is wrong because Java truncates the decimal part. Cast or use a double value when a decimal result is needed.
Practice Questions
- 1 What is the value of result after this code runs: int result = 17 / 5 + 17 % 5;?
- 2 An array is declared as int[] nums = {4, 7, 2, 9};. What value is stored in nums[2], and what is nums.length?
- 3 What is printed by this code: String s = "computer"; System.out.println(s.substring(1, 4));?
- 4 Explain why an enhanced for loop is a good choice for finding the sum of all values in an array, but not a good choice for replacing each value with a new value.