AP Computer Science A Exam — Questions and Answers
Question 1: How many times will the following loop execute?
- 4
- 6
- Infinite
- 5 (Correct answer)
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 2: What is the default value of elements in a newly created `int[]` array?
- 1
- -1
- null
- 0 (Correct answer)
Correct answer: 0
Java initializes all elements of a numeric array to 0 by default when created with `new`.
Question 3: What is the output if `System.out.print(n+" ");` is moved AFTER `count(n-1);` in the previous question?
- 3 2 1
- 3 2 1 0
- 1 2 3 (Correct answer)
- 0 1 2
Correct answer: 1 2 3
When the print is after the recursive call, execution prints on the way back up the call stack, giving ascending order.
Question 4: What is the best-case time complexity of bubble sort when the array is already sorted?
- O(log n)
- O(1)
- O(n²)
- 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 5: What is traversing a 2D array row by row called?
- Column-major order traversal
- Spiral traversal
- Row-major order traversal (Correct answer)
- Diagonal traversal
Correct answer: Row-major order traversal
Iterating through a 2D array row by row (outer loop over rows) is called row-major order traversal.
Question 6: What does `str.indexOf("lo")` return for `str = "Hello"`?
- 3 (Correct answer)
- 0
- -1
- 2
Correct answer: 3
indexOf returns the starting index of the first occurrence of the substring; "lo" starts at index 3 in "Hello".
Question 7: Which concept does recursion naturally model when processing nested data structures like trees?
- Iteration
- Linear search
- Divide and conquer (Correct answer)
- Parallel processing
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 8: What is true about abstract classes in Java?
- An abstract class must have at least one abstract method.
- An abstract class cannot be instantiated. (Correct answer)
- A class that extends an abstract class must implement all its abstract methods. (Correct answer)
- Abstract classes can have both abstract and non-abstract methods. (Correct answer)
Correct answer: An abstract class cannot be instantiated.
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 9: What does `Arrays.sort(arr)` use internally in Java for primitive arrays?
- Bubble sort
- A variant of quicksort (dual-pivot quicksort) (Correct answer)
- Merge sort
- Insertion sort
Correct answer: A variant of quicksort (dual-pivot quicksort)
Java's Arrays.sort() for primitives uses a dual-pivot quicksort, which provides excellent average-case performance in practice.
Question 10: 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)
- 1 2 3
- 3 2 1 0
- 0 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 11: What will the following code output?
- A and B (Correct answer)
- C
- A
- B
- A and C
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 12: Can a subclass inherit from multiple superclasses in Java?
- No, Java only supports single inheritance for classes (Correct answer)
- Yes, using the extends keyword multiple times
- Yes, using the implements keyword for classes
- No, and interfaces also don't support multiple inheritance
Correct answer: No, Java only supports single inheritance for classes
Java supports single class inheritance only — a class can extend only one superclass, though it can implement multiple interfaces.
Question 13: What is the output of the following code?
- Bark (Correct answer)
- Error: Animal cannot refer to Dog.
- Both Some sound and Bark
- Some sound
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 14: Which ArrayList method removes the first occurrence of a specified object?
- delete(Object o)
- erase(Object o)
- remove(Object o) (Correct answer)
- removeFirst(Object o)
Correct answer: remove(Object o)
ArrayList.remove(Object o) removes the first occurrence of the specified element from the list.
Question 15: In the Fibonacci sequence defined recursively as fib(n)=fib(n-1)+fib(n-2), what are the base cases?
- fib(0)=1 and fib(1)=1
- fib(1)=1 only
- fib(0)=0 only
- fib(0)=0 and fib(1)=1 (Correct answer)
Correct answer: fib(0)=0 and fib(1)=1
The standard recursive Fibonacci has two base cases: fib(0)=0 and fib(1)=1, terminating the recursion.
Question 16: How does encapsulation improve code quality?
- By hiding the implementation details of a class. (Correct answer)
- By making all class variables public.
- By ensuring all methods are static.
- By using getters and setters to control access to variables. (Correct answer)
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 17: 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)
- static
- intern
- final
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 18: What is the key operation that makes binary search efficient compared to linear search?
- Comparing adjacent elements
- Eliminating half the remaining elements with each comparison (Correct answer)
- Sorting the array first
- Using recursion instead of loops
Correct answer: Eliminating half the remaining elements with each comparison
Binary search eliminates half the search space with each comparison by using the sorted order to discard the irrelevant half.
Question 19: What is the index of the first element in a Java array?
- 0 (Correct answer)
- -1
- 1
- Depends on declaration
Correct answer: 0
Java arrays are zero-indexed, so the first element is always at index 0.
Question 20: Which method adds an element to the end of an ArrayList?
- insert()
- add() (Correct answer)
- push()
- append()
Correct answer: add()
The ArrayList method add(element) appends the specified element to the end of the list.
Question 21: Which of these correctly initializes a 2D array with 3 rows and 4 columns?
- int[][] grid = new int[3][4]; (Correct answer)
- int[3][4] grid = new int[][];
- int grid[3][4];
- int[][] grid = new int[4][3];
Correct answer: int[][] grid = new int[3][4];
The syntax `new int[rows][columns]` creates a 2D array; `new int[3][4]` gives 3 rows and 4 columns.
Question 22: Which of the following correctly initializes an array in Java?
- int[] arr = new int[]{1, 2, 3}; (Correct answer)
- int arr = {1, 2, 3};
- int arr = {1, 2, 3}; (Correct answer)
- int[] arr = new int[3]; (Correct answer)
- int arr = new int[3]{1, 2, 3};
Correct answer: int[] arr = new int[]{1, 2, 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 23: In recursion, each method call gets its own set of local variables stored where?
- In a global variable table
- In a static memory block
- In the heap
- On the call stack (Correct answer)
Correct answer: On the call stack
Each recursive call creates a new stack frame on the call stack that holds its own local variables and parameters.
Question 24: What will be the output of the following code?
- A (Correct answer)
- No output
- B
- Compilation error
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 25: How do you fill an entire 2D array `grid` of size n×m with the value 0?
- Use grid.fill(0)
- Declare it as `int[][] grid = new int[n][m]` (auto-initialized to 0) (Correct answer)
- Use Arrays.fill(grid, 0)
- Use nested for loops assigning grid[i][j] = 0
Correct answer: Declare it as `int[][] grid = new int[n][m]` (auto-initialized to 0)
Java automatically initializes int array elements to 0, so `new int[n][m]` creates a 2D array already filled with zeros.
Question 26: In AP CSA, which searching algorithm is commonly implemented recursively?
- Linear search
- Binary search (Correct answer)
- Selection sort
- Bubble sort
Correct answer: Binary search
Binary search is naturally implemented recursively by halving the search space with each call until the target is found or the range is empty.
Question 27: What is the output of the following code?
- AP
- Error
- PCSA
- APCSA
- CSA (Correct answer)
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 28: What does `str.contains("ell")` return for `str = "Hello"`?
- true (Correct answer)
- 1
- 'e'
- false
Correct answer: true
contains() returns true if the specified sequence appears anywhere within the String; "ell" is found within "Hello".
Question 29: What will be the output of this code?
- Wednesday Invalid day (Correct answer)
- Error
- Wednesday
- Invalid day
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 30: Which of the following statements are true about method overloading and overriding?
- Overloading occurs within the same class. (Correct answer)
- Overridden methods must have the same method signature. (Correct answer)
- Overloaded methods must have different method names.
- Overriding requires inheritance. (Correct answer)
Correct answer: Overloading occurs within the same class.
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 31: What does `list.remove(0)` do to an ArrayList?
- Throws an exception
- Removes all elements equal to 0
- Removes the element at index 0 (Correct answer)
- Removes the last element
Correct answer: Removes the element at index 0
When passed an int, ArrayList's remove(int index) removes the element at that index, shifting remaining elements left.
Question 32: What is the output of the following code?
- 0,0 1,0
- 0,0 0,1 0,2
- 0,0 0,1 1,0 1,1
- 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 33: What is the correct way to create an object from the following class?
- Car myCar = new Car(String color);
- Car myCar = new Car();
- Car myCar = Car("Red");
- 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 34: What is the base case in a recursive method?
- The first recursive call
- The method signature
- The return type of the method
- The condition that stops the recursion (Correct answer)
Correct answer: The condition that stops the recursion
The base case is the condition that terminates recursion by returning a result without making further recursive calls.
Question 35: What is the output of: `int[] a = {10, 20, 30}; for(int x : a) System.out.print(x + " ");`
- 30 20 10
- 10, 20, 30,
- 10 20 30 (Correct answer)
- 0 1 2
Correct answer: 10 20 30
The enhanced for loop assigns each element of the array to x in order, printing 10, 20, then 30.
Question 36: Consider: `public abstract class Shape { public abstract double area(); }`. Which of the following correctly creates a subclass?
- public abstract class Circle extends Shape { }
- public class Circle extends Shape { }
- public class Circle extends Shape { public double area() { return 3.14 * r * r; } } (Correct answer)
- public class Circle implements Shape { public double area() { return 3.14 * r * r; } }
Correct answer: public class Circle extends Shape { public double area() { return 3.14 * r * r; } }
Circle must extend Shape (not implement) and must provide a concrete `area()` method to avoid being abstract itself.
Question 37: How many comparisons does binary search make in the worst case on an array of 16 elements?
- 8
- 16
- 2
- 4 (Correct answer)
Correct answer: 4
Binary search on 16 elements: 16→8→4→2→1, taking log₂(16) = 4 comparisons in the worst case.
Question 38: How many total method calls does factorial(4) make (including the initial call)?
- 3
- 5 (Correct answer)
- 6
- 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 39: In the AP CSA exam, what is the standard way to search for a value in an unsorted ArrayList?
- Sequential (linear) search (Correct answer)
- Binary search
- Hash lookup
- Interpolation 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 40: What keyword does a subclass use to inherit from a superclass in Java?
- implements
- inherits
- super
- extends (Correct answer)
Correct answer: extends
In Java, a subclass uses the `extends` keyword to inherit fields and methods from its superclass.
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