AP Computer Science A Exam — Questions and Answers
Question 1: What nested loop structure traverses all elements of a 2D array `int[][] m` with r rows and c columns?
- for(int i=0;i<=m.length;i++) for(int j=0;j<=m[i].length;j++)
- for(int i=1;i<m.length;i++) for(int j=1;j<m[i].length;j++)
- for(int i=0;i<m[0].length;i++) for(int j=0;j<m.length;j++)
- for(int i=0;i<m.length;i++) for(int j=0;j<m[i].length;j++) (Correct answer)
Correct answer: for(int i=0;i<m.length;i++) for(int j=0;j<m[i].length;j++)
The outer loop uses m.length for rows and the inner loop uses m[i].length for columns, correctly visiting every element.
Question 2: Which of the following statements are true about method overloading and overriding?
- Overridden methods must have the same method signature. (Correct answer)
- Overloading occurs within the same class. (Correct answer)
- Overloaded methods must have different method names.
- Overriding requires inheritance. (Correct answer)
Correct answer: Overridden methods must have the same method signature.
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 3: Which method adds an element to the end of an ArrayList?
- insert()
- add() (Correct answer)
- append()
- push()
Correct answer: add()
The ArrayList method add(element) appends the specified element to the end of the list.
Question 4: Which of the following best describes the purpose of an interface in Java?
- To allow direct instantiation with default behaviors
- To define a contract specifying what methods a class must implement (Correct answer)
- To prevent a class from being instantiated
- To provide a partial implementation that subclasses extend
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 5: In insertion sort, how are elements processed?
- The minimum is selected and moved front
- Each element is inserted into its correct position among already-sorted elements (Correct answer)
- Adjacent elements are swapped if out of order
- The array is recursively divided
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 6: A class Truck extends an abstract class Vehicle that has abstract methods `start()` and `stop()`. Truck provides `start()` but NOT `stop()`. What must be true of Truck?
- Truck compiles fine because it provided at least one method
- Truck automatically inherits a default `stop()` from Vehicle
- Truck must also be declared abstract (Correct answer)
- A runtime error occurs when `stop()` is called
Correct answer: Truck must also be declared abstract
Since Truck does not implement all abstract methods inherited from Vehicle, Truck itself must be declared abstract.
Question 7: How do you find the number of elements in an ArrayList called `list`?
- list.length()
- list.length
- list.count()
- list.size() (Correct answer)
Correct answer: list.size()
ArrayList uses the size() method (not length) to return the number of elements it contains.
Question 8: What will be the output of this code?
- Wednesday
- Invalid day
- Wednesday Invalid day (Correct answer)
- Error
Correct answer: Wednesday Invalid day
The `switch` statement evaluates the `day` variable, which is "Wednesday". It matches the `case "Wednesday"`, causing "Wednesday" to be printed. Crucially, there is no `break` statement after this `case` block, leading to 'fall-through'. This means the execution continues to the `default` block, which then prints "Invalid day" immediately after "Wednesday".
Question 9: What keyword makes a String variable refer to a string literal shared in the string pool?
- No special keyword — string literals are automatically pooled (Correct answer)
- final
- intern
- static
Correct answer: No special keyword — string literals are automatically pooled
Java automatically places String literals in the string pool; no keyword is needed, though intern() can force pool placement for non-literal strings.
Question 10: What is the output of the following code?
- APCSA
- CSA (Correct answer)
- PCSA
- Error
- AP
Correct answer: CSA
The `substring(int beginIndex)` method in Java returns a new string that starts at the specified index and extends to the end of the original string. In the string "APCSA", the character 'A' is at index 0, 'P' at index 1, 'C' at index 2, 'S' at index 3, and the final 'A' at index 4. Therefore, `str.substring(2)` will start at index 2 ('C') and include all subsequent characters, resulting in "CSA".
Question 11: Which of the following is NOT an advantage of recursion?
- Can simplify code for problems with recursive structure
- Natural fit for tree traversal
- Models mathematical induction directly
- Always more memory-efficient than iteration (Correct answer)
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 12: Can an abstract class contain non-abstract (concrete) methods?
- Yes, but only if it also implements an interface
- No, concrete methods belong only in interfaces
- No, all methods in an abstract class must be abstract
- Yes, it can contain both abstract and concrete methods (Correct answer)
Correct answer: Yes, it can contain both abstract and concrete methods
An abstract class can have a mix of abstract methods (no body) and concrete methods (with a body), providing partial implementation.
Question 13: Which of the following correctly converts an ArrayList<Integer> to an array?
- Arrays.toArray(list)
- list.toArray(new Integer[0]) (Correct answer)
- list.asArray()
- list.toArray()
Correct answer: list.toArray(new Integer[0])
The toArray(T[] a) method of ArrayList returns an array containing all elements; passing `new Integer[0]` specifies the type.
Question 14: What is the output of this code?
- 0 1 2 3 4
- 0 1 2 (Correct answer)
- 3 4
- 0 1 2 3
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 15: What is a recursive method that counts down from n printing each number called?
- Tail recursion
- Mutual recursion
- Linear recursion (Correct answer)
- Binary recursion
Correct answer: Linear recursion
A method making a single recursive call per invocation, working through a linear sequence, is called linear recursion.
Question 16: How many total method calls does factorial(4) make (including the initial call)?
- 5 (Correct answer)
- 3
- 4
- 6
Correct answer: 5
factorial(4) calls factorial(3), which calls factorial(2), factorial(1), and factorial(0) — 5 calls total including the first.
Question 17: What must a concrete (non-abstract) subclass do with abstract methods it inherits?
- Declare them abstract again
- Delete them
- Ignore them
- Implement (override) all of them (Correct answer)
Correct answer: Implement (override) all of them
A concrete subclass must provide implementations for all abstract methods inherited from its abstract superclass, or itself be declared abstract.
Question 18: Which of the following best describes mutual recursion?
- A method calling itself twice
- A method with two base cases
- A loop that calls a recursive method
- Two methods that each call the other (Correct answer)
Correct answer: Two methods that each call the other
Mutual recursion occurs when method A calls method B and method B calls method A, forming a cycle.
Question 19: What does `str.indexOf("lo")` return for `str = "Hello"`?
- 2
- 3 (Correct answer)
- -1
- 0
Correct answer: 3
indexOf returns the starting index of the first occurrence of the substring; "lo" starts at index 3 in "Hello".
Question 20: What is the recursive case?
- The parameter declaration
- The stopping condition
- The method's return statement
- The part of the method that makes a call to itself with a smaller/simpler input (Correct answer)
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 21: By default, all methods declared inside an interface are:
- protected and final
- public and abstract (Correct answer)
- private and abstract
- public and static
Correct answer: public and abstract
Interface methods are implicitly public and abstract, meaning any implementing class must provide a concrete implementation.
Question 22: Which of the following are primitive data types in Java?
- int (Correct answer)
- char (Correct answer)
- String
- boolean (Correct answer)
- ArrayList
Correct answer: int
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 23: What is 'unwinding the stack' in recursion?
- The process of making recursive calls deeper
- Throwing an exception from a recursive call
- Clearing all local variables
- The process of returning from recursive calls back to the original caller (Correct answer)
Correct answer: The process of returning from recursive calls back to the original caller
Stack unwinding is when recursive calls finish and return in reverse order, passing values back up to the original caller.
Question 24: What will the following code output?
- C
- B
- A and 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 25: Which concept does recursion naturally model when processing nested data structures like trees?
- Divide and conquer (Correct answer)
- Parallel processing
- Linear search
- 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 26: How many times will the following loop execute?
- 5 (Correct answer)
- 6
- 4
- 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 27: What does `Collections.sort(list)` require of the objects in the list?
- They must be in reverse order first
- They must implement the Comparable interface (Correct answer)
- They must be integers
- They must have a length() method
Correct answer: They must implement the Comparable interface
Collections.sort() uses the natural ordering defined by the Comparable interface's compareTo method on the list's elements.
Question 28: What does the following return? `public int f(int n) { if(n==0) return 0; return n + f(n-1); }` called with f(4)?
- 0
- 24
- 4
- 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 29: Which sorting algorithm repeatedly finds the minimum element and places it at the beginning?
- Selection sort (Correct answer)
- Merge sort
- Bubble sort
- Insertion sort
Correct answer: Selection sort
Selection sort works by finding the smallest unsorted element and swapping it to its correct sorted position in each pass.
Question 30: How does encapsulation improve code quality?
- By hiding the implementation details of a class. (Correct answer)
- By making all class variables public.
- By using getters and setters to control access to variables. (Correct answer)
- By ensuring all methods are static.
Correct answer: By hiding the implementation details of a class.
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 31: What keyword does a subclass use to inherit from a superclass in Java?
- inherits
- extends (Correct answer)
- implements
- super
Correct answer: extends
In Java, a subclass uses the `extends` keyword to inherit fields and methods from its superclass.
Question 32: 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 (Correct answer)
- 3 2 1 0
- 0 1 2 3
- 1 2 3
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 33: When encountering a challenging question on the AP CSA multiple-choice section, what should you do?
- Guess if unsure, as there is no penalty for wrong answers. (Correct answer)
- Eliminate obviously wrong choices first. (Correct answer)
- Spend extra time to ensure you get it it correct.
- Skip it an return later. (Correct answer)
Correct answer: Guess if unsure, as there is no penalty for wrong answers.
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 34: Which ArrayList method removes the first occurrence of a specified object?
- removeFirst(Object o)
- delete(Object o)
- erase(Object o)
- remove(Object o) (Correct answer)
Correct answer: remove(Object o)
ArrayList.remove(Object o) removes the first occurrence of the specified element from the list.
Question 35: What is the correct way to create an object from the following class?
- Car myCar = Car("Red");
- Car myCar = new Car();
- Car myCar = new Car(String color);
- Car myCar = new Car("Red"); (Correct answer)
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.
Question 36: What is the output of the following code?
- Both Some sound and Bark
- Some sound
- Bark (Correct answer)
- Error: Animal cannot refer to Dog.
Correct answer: Bark
This code demonstrates polymorphism and method overriding. An `Animal` reference `myAnimal` is assigned a `Dog` object. When `myAnimal.makeSound()` is called, Java's runtime polymorphism ensures that the specific `makeSound()` method implemented in the `Dog` class (the actual object type) is invoked, rather than the `Animal` class's method. Therefore, the output will be "Bark".
Question 37: In the AP CSA exam, what is the standard way to search for a value in an unsorted ArrayList?
- Sequential (linear) search (Correct answer)
- Interpolation search
- Hash lookup
- Binary search
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 38: If class Dog extends Animal and both have a `speak()` method, what does `Animal a = new Dog(); a.speak();` call?
- Both methods in sequence
- A compile error
- Dog's speak() method (Correct answer)
- Animal's speak() method
Correct answer: Dog's speak() method
Due to dynamic dispatch, the JVM looks at the actual object type (Dog) at runtime and calls Dog's speak() method, not Animal's.
Question 39: Which keyword is used to declare an abstract class in Java?
- interface
- sealed
- virtual
- abstract (Correct answer)
Correct answer: abstract
The `abstract` keyword is placed before the `class` keyword to declare a class as abstract, meaning it cannot be instantiated directly.
Question 40: How many times will the following loop execute?
- 6
- 4
- 5 (Correct answer)
- Infinite
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.
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