Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

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. 1 What is the value of result after this code runs: int result = 17 / 5 + 17 % 5;?
  2. 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. 3 What is printed by this code: String s = "computer"; System.out.println(s.substring(1, 4));?
  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.

Understanding AP Computer Science A Java Reference

Java programs are easiest to understand when you follow the state of the program as it runs. State means the current values stored in variables and the current contents of objects. A local variable exists only while its method is running.

An instance variable belongs to each individual object, so two objects made from one class can hold different data. This distinction matters when a method changes a field.

The change remains after the method ends because the object still exists. A method that only changes one of its local variables does not change the caller's variable.

Object variables do not contain a whole object inside them. They hold a reference to an object somewhere in memory. If two variables refer to the same object, a change through one variable can be seen through the other.

This is called aliasing, and it explains many confusing trace problems. Strings behave differently from mutable collections. A String cannot be changed in place.

Methods that seem to modify text produce a new String, so the result must be saved if it is needed later. Students should watch for null references too. A variable with null does not refer to an object, so calling a method through it causes an error.

Arrays and ArrayLists require careful attention to positions. Their first position is zero, which means the final valid position is one less than the number of elements. A loop can be logically correct but still fail by visiting one position too far.

Removing an item from an ArrayList shifts later items toward the front. For this reason, a loop that removes several items often needs to move from the end toward the beginning.

Inserting items shifts elements in the other direction. Enhanced loops are useful for reading values, but indexed loops give control when a task depends on location, replacement, or removal.

Free response work is usually graded for precise behavior, not for clever code. Read every method description as a contract. Identify what data enters the method, what must be returned or changed, and which cases need separate handling.

Empty collections, one element, repeated values, and boundary positions are common test cases. For inheritance questions, separate the behavior promised by the parent class from behavior added by the child class.

An overridden method is chosen using the actual object type at runtime, even when a variable is declared using a parent type. Tracing a few lines by hand before writing code helps reveal wrong assumptions about values, references, loop limits, and return statements.