AP Computer Science A Exam — Questions and Answers
Question 1: Which ArrayList method removes the first occurrence of a specified object?
- delete(Object o)
- remove(Object o) (Correct answer)
- removeFirst(Object o)
- erase(Object o)
Correct answer: remove(Object o)
ArrayList.remove(Object o) removes the first occurrence of the specified element from the list.
Question 2: What will be the output of the following code?
- B
- Compilation error
- A (Correct answer)
- No output
Correct answer: A
The code initializes an integer `x` with the value `10`. The `if` condition `(x > 5)` evaluates to `true` because `10` is indeed greater than `5`. Consequently, the statement inside the `if` block, `System.out.print("A")`, is executed, printing "A" to the console. The `else` block is skipped.
Question 3: What is a 'has-a' relationship in OOP (as opposed to 'is-a')?
- Overriding — one class replaces a method
- Inheritance — one class extends another
- Composition — one class contains a reference to another class (Correct answer)
- Abstraction — hiding implementation details
Correct answer: Composition — one class contains a reference to another class
A 'has-a' relationship is composition: a class holds a reference to another class as a field, rather than inheriting from it.
Question 4: Which of the following sorts has average-case time complexity of O(n log n)?
- Insertion sort
- Merge sort (Correct answer)
- Bubble sort
- Selection sort
Correct answer: Merge sort
Merge sort divides the array in half recursively and merges in O(n) time per level, giving O(n log n) overall.
Question 5: What is the output of the following code?
- 0,0 0,1 1,0 1,1
- 0,0 0,1 0,2
- 0,0 1,0
- 0,0 0,1 0,2 1,0 1,1 1,2 (Correct answer)
Correct answer: 0,0 0,1 0,2 1,0 1,1 1,2
This code uses nested `for` loops. The outer loop iterates for `i = 0` and `i = 1`. For each iteration of the outer loop, the inner loop completes its full cycle, iterating for `j = 0, 1, 2`. This results in the output: `0,0 0,1 0,2` (for `i=0`) followed by `1,0 1,1 1,2` (for `i=1`).
Question 6: Which of the following are primitive data types in Java?
- char (Correct answer)
- String
- int (Correct answer)
- ArrayList
- boolean (Correct answer)
Correct answer: char
In Java, primitive data types are fundamental data types that store simple values directly in memory. `int` is used for whole numbers, `boolean` for true/false values, and `char` for single characters. `String` and `ArrayList` are reference types (objects), not primitives.
Question 7: What is the output of: `public void count(int n){ if(n==0) return; System.out.print(n+" "); count(n-1); }` called with count(3)?
- 3 2 1 0
- 0 1 2 3
- 1 2 3
- 3 2 1 (Correct answer)
Correct answer: 3 2 1
count(3) prints 3, then calls count(2) which prints 2, then count(1) which prints 1, then count(0) returns.
Question 8: What is the recursive case?
- The parameter declaration
- The method's return statement
- The part of the method that makes a call to itself with a smaller/simpler input (Correct answer)
- The stopping condition
Correct answer: The part of the method that makes a call to itself with a smaller/simpler input
The recursive case is the branch that calls the method again with a modified argument, moving toward the base case.
Question 9: What is the role of an interface in Java's type system for AP CSA purposes?
- It defines a contract of methods that implementing classes must provide (Correct answer)
- It provides a default implementation for all methods
- It is a class that cannot be instantiated
- It replaces the need for inheritance
Correct answer: It defines a contract of methods that implementing classes must provide
A Java interface specifies a set of method signatures that any implementing class must define, establishing a contract without providing implementations.
Question 10: What does it mean for a class to be `abstract`?
- It is the superclass of all classes
- It has no fields
- It cannot be instantiated and may contain abstract methods (Correct answer)
- It cannot have any methods
Correct answer: It cannot be instantiated and may contain abstract methods
An abstract class cannot be instantiated directly and can declare abstract methods that subclasses must implement.
Question 11: What is the default value of elements in a newly created `int[]` array?
- 0 (Correct answer)
- 1
- -1
- null
Correct answer: 0
Java initializes all elements of a numeric array to 0 by default when created with `new`.
Question 12: What is the time complexity of a simple linear recursion that calls itself once per step from n down to 0?
- O(n) (Correct answer)
- O(n²)
- O(log n)
- O(1)
Correct answer: O(n)
A recursion that makes one call per level and counts down from n to 0 executes n+1 calls, giving O(n) time complexity.
Question 13: In insertion sort, how are elements processed?
- Each element is inserted into its correct position among already-sorted elements (Correct answer)
- The minimum is selected and moved front
- The array is recursively divided
- Adjacent elements are swapped if out of order
Correct answer: Each element is inserted into its correct position among already-sorted elements
Insertion sort builds a sorted portion by taking each new element and inserting it at the correct position in the already-sorted left portion.
Question 14: What will the following code output?
- A and C
- B
- C
- A
- A and B (Correct answer)
Correct answer: A and B
This code snippet likely contains two separate `if` statements, not an `if-else if-else` chain. If `x` is initialized to 10, the first `if (x > 5)` condition (10 > 5) is true, so 'A' is printed. The second `if (x < 15)` condition (10 < 15) is also true, so 'B' is printed. The `else` block associated with the second `if` is skipped.
Question 15: How many total method calls does factorial(4) make (including the initial call)?
- 5 (Correct answer)
- 6
- 3
- 4
Correct answer: 5
factorial(4) calls factorial(3), which calls factorial(2), factorial(1), and factorial(0) — 5 calls total including the first.
Question 16: How does encapsulation improve code quality?
- By making all class variables public.
- By ensuring all methods are static.
- By using getters and setters to control access to variables. (Correct answer)
- By hiding the implementation details of a class. (Correct answer)
Correct answer: By using getters and setters to control access to variables.
Encapsulation is a core OOP principle that enhances code quality by bundling data and methods within a class and controlling access to its internal state. It primarily achieves this by hiding the implementation details of a class (e.g., making variables private) and providing public methods (getters and setters) for controlled interaction. This protects data integrity, reduces complexity, and makes code more modular and maintainable.
Question 17: Which concept does recursion naturally model when processing nested data structures like trees?
- Linear search
- Parallel processing
- Divide and conquer (Correct answer)
- Iteration
Correct answer: Divide and conquer
Recursion naturally models divide and conquer — splitting a problem into subproblems of the same type, as with tree traversal.
Question 18: Which sorting algorithm is most efficient for nearly-sorted arrays?
- Insertion sort (Correct answer)
- Merge sort
- Bubble sort
- Selection sort
Correct answer: Insertion sort
Insertion sort performs very few swaps on nearly-sorted data and approaches O(n) performance in that case.
Question 19: How many times is the base case reached when computing factorial(3) recursively?
- 1 (Correct answer)
- 0
- 3
- 4
Correct answer: 1
factorial(3) calls factorial(2) which calls factorial(1) which calls factorial(0) — the base case is reached exactly once.
Question 20: What exception is thrown when you access an array index that is out of bounds?
- IndexOutOfRangeException
- NullPointerException
- IllegalArgumentException
- ArrayIndexOutOfBoundsException (Correct answer)
Correct answer: ArrayIndexOutOfBoundsException
Java throws ArrayIndexOutOfBoundsException when you access a negative index or an index >= array length.
Question 21: Which of the following statements are true about method overloading and overriding?
- Overriding requires inheritance. (Correct answer)
- Overloaded methods must have different method names.
- Overridden methods must have the same method signature. (Correct answer)
- Overloading occurs within the same class. (Correct answer)
Correct answer: Overriding requires inheritance.
Method overloading occurs within the same class when multiple methods share the same name but have different parameter lists (different method signatures). Method overriding, on the other hand, requires inheritance, where a subclass provides a specific implementation for a method already defined in its superclass. For a method to be overridden, it must have the exact same method signature (name, return type, and parameter list) as the method in the superclass.
Question 22: When encountering a challenging question on the AP CSA multiple-choice section, what should you do?
- Eliminate obviously wrong choices first. (Correct answer)
- Skip it an return later. (Correct answer)
- Spend extra time to ensure you get it it correct.
- Guess if unsure, as there is no penalty for wrong answers. (Correct answer)
Correct answer: Eliminate obviously wrong choices first.
When encountering a challenging question on a multiple-choice exam, skipping it and returning later is an effective time management strategy. This prevents you from getting stuck on one question, allowing you to answer other questions you know first. You can then revisit the difficult question with a fresh perspective or after having gained confidence from answering other items.
Question 23: How do you compare two Strings for equal content in Java?
- str1.equals(str2) (Correct answer)
- str1 == str2
- str1.equalsTo(str2)
- str1.compareTo(str2) == 0 (also valid but equals is standard)
Correct answer: str1.equals(str2)
The equals() method compares String content; using == compares object references, which may give unexpected results for Strings.
Question 24: What does `str.indexOf("lo")` return for `str = "Hello"`?
- -1
- 0
- 2
- 3 (Correct answer)
Correct answer: 3
indexOf returns the starting index of the first occurrence of the substring; "lo" starts at index 3 in "Hello".
Question 25: How many times will the following loop execute?
- 6
- 4
- 5 (Correct answer)
- Infinite
Correct answer: 5
The `for` loop initializes `i` to 0. The loop continues as long as the condition `i < 5` is true. The values `i` will take are 0, 1, 2, 3, and 4. When `i` becomes 5, the condition `i < 5` is false, and the loop terminates. Therefore, the loop body executes a total of 5 times.
Question 26: What is the best-case time complexity of bubble sort when the array is already sorted?
- O(log n)
- O(n²)
- O(1)
- O(n) (Correct answer)
Correct answer: O(n)
An optimized bubble sort can detect no swaps occurred in a pass and terminate early, giving O(n) for an already-sorted array.
Question 27: Which of the following is NOT an advantage of recursion?
- Models mathematical induction directly
- Can simplify code for problems with recursive structure
- Always more memory-efficient than iteration (Correct answer)
- Natural fit for tree traversal
Correct answer: Always more memory-efficient than iteration
Recursion is not always more memory-efficient; each call uses stack space, so deep recursion can use more memory than iteration.
Question 28: What is a common use of the `instanceof` operator in polymorphism?
- To create a new object of a given type
- To call a superclass method
- To check the actual runtime type of an object before a downcast (Correct answer)
- To check if two objects are equal
Correct answer: To check the actual runtime type of an object before a downcast
`instanceof` tests whether an object is an instance of a specific class, commonly used before casting a superclass reference to a subclass type.
Question 29: Which of the following best describes the purpose of an interface in Java?
- To provide a partial implementation that subclasses extend
- To define a contract specifying what methods a class must implement (Correct answer)
- To allow direct instantiation with default behaviors
- To prevent a class from being instantiated
Correct answer: To define a contract specifying what methods a class must implement
An interface defines a contract — a set of method signatures — that any implementing class must fulfill, promoting abstraction and polymorphism.
Question 30: What is the output of this code?
- 0 1 2 3 4
- 0 1 2 3
- 3 4
- 0 1 2 (Correct answer)
Correct answer: 0 1 2
The `for` loop initializes `i` to `0`. In each iteration, `i` is printed, followed by a space. When `i` becomes `3`, the `if (i == 3)` condition is met, and the `break` statement is executed. The `break` immediately terminates the loop, preventing `3` and any subsequent numbers from being printed.
Question 31: What does `ArrayList<Integer> list = new ArrayList<>();` create?
- An ArrayList pre-filled with zeros
- An empty ArrayList that holds Integer objects (Correct answer)
- A fixed-size list of integers
- An ArrayList of primitive ints
Correct answer: An empty ArrayList that holds Integer objects
This statement creates an empty generic ArrayList that can store Integer wrapper objects.
Question 32: What does the following return? `public int f(int n) { if(n==0) return 0; return n + f(n-1); }` called with f(4)?
- 4
- 24
- 0
- 10 (Correct answer)
Correct answer: 10
f(4) = 4 + f(3) = 4+3+2+1+0 = 10, computing the sum of integers from 0 to n.
Question 33: Can a single class in Java implement more than one interface?
- Yes, by listing them separated by commas after implements (Correct answer)
- No, Java only allows one interface per class
- No, multiple interfaces require an abstract class
- Yes, but only if they share no method names
Correct answer: Yes, by listing them separated by commas after implements
Java allows a class to implement multiple interfaces by listing them after the `implements` keyword, separated by commas.
Question 34: In the AP CSA exam, what is the standard way to search for a value in an unsorted ArrayList?
- Interpolation search
- Binary search
- Sequential (linear) search (Correct answer)
- Hash lookup
Correct answer: Sequential (linear) search
An unsorted ArrayList requires sequential search since binary search requires sorted data, and ArrayList has no built-in hash lookup.
Question 35: Which of the following correctly accesses the last element of an array `arr` of length n?
- arr[length-1]
- arr[n]
- arr[last]
- arr[n-1] (Correct answer)
Correct answer: arr[n-1]
Since arrays are zero-indexed, the last element is at index n-1 where n is the array's length.
Question 36: What is true about abstract classes in Java?
- Abstract classes can have both abstract and non-abstract methods. (Correct answer)
- A class that extends an abstract class must implement all its abstract methods. (Correct answer)
- An abstract class must have at least one abstract method.
- An abstract class cannot be instantiated. (Correct answer)
Correct answer: Abstract classes can have both abstract and non-abstract methods.
Abstract classes in Java serve as blueprints for other classes and cannot be instantiated directly. They can contain a combination of abstract methods (declared without an implementation) and concrete methods (with full implementations). Any concrete class that extends an abstract class must provide implementations for all the abstract methods inherited from the abstract class, or it must also be declared abstract itself.
Question 37: What is the result of `int[] arr = {3, 1, 4, 1, 5}; System.out.println(arr[2]);`?
- 1
- 3
- 4 (Correct answer)
- 5
Correct answer: 4
arr[2] accesses the third element (index 2), which is 4 in the array {3, 1, 4, 1, 5}.
Question 38: How many times will the following loop execute?
- 6
- 5 (Correct answer)
- Infinite
- 4
Correct answer: 5
The `for` loop initializes `i` to `0` and continues as long as `i` is less than `5`. The loop iterates for `i` values `0, 1, 2, 3, 4`. Therefore, the loop body will execute exactly five times, once for each of these values.
Question 39: Which of the following correctly initializes an array in Java?
- int[] arr = new int[3]; (Correct answer)
- int arr = {1, 2, 3};
- int[] arr = new int[]{1, 2, 3}; (Correct answer)
- int arr = new int[3]{1, 2, 3};
- int arr = {1, 2, 3}; (Correct answer)
Correct answer: int[] arr = new int[3];
The syntax `int[] arr = {1, 2, 3};` is a valid and common shorthand in Java for declaring an array of integers and initializing it with the specified values. The `[]` after `int` correctly indicates that `arr` is an array type. (Note: The provided correct answer `int arr = {1, 2, 3};` is missing the `[]` for array declaration and would be a compile error in standard Java; assuming it was a typo and `int[] arr = {1, 2, 3};` was intended.)
Question 40: What is the correct way to create an object from the following class?
- Car myCar = new Car(String color);
- Car myCar = new Car("Red"); (Correct answer)
- Car myCar = Car("Red");
- Car myCar = new Car();
Correct answer: Car myCar = new Car("Red");
To create an object in Java, you use the `new` keyword followed by a call to the class's constructor. The `Car` class has a constructor `Car(String color)` that requires a `String` argument. Therefore, `Car myCar = new Car("Red");` correctly instantiates a `Car` object, passing "Red" as the color, and assigns it to the `myCar` variable.
AP Computer Science A Exam
The AP Computer Science A exam tests students on Java programming fundamentals including object-oriented design, data structures, algorithms, and control flow. The 2025-2026 exam follows a revised 4-unit curriculum delivered digitally via College Board Bluebook.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds