AP Computer Science A Exam — Questions and Answers
Question 1: 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 = new Car("Red"); (Correct answer)
- Car myCar = Car("Red");
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 2: Which method returns the number of characters in a String in Java?
- count()
- charAt()
- length() (Correct answer)
- size()
Correct answer: length()
The String.length() method returns the number of characters (including spaces) in the string.
Question 3: What happens if you try to store a double value in an int array in Java?
- The value is automatically truncated
- The array resizes
- Runtime error
- Compile-time error (Correct answer)
Correct answer: Compile-time error
Java is strongly typed; storing a double in an int array causes a compile-time type mismatch error.
Question 4: Which of the following are effective ways to prepare for AP CSA exam?
- Memorize Java syntax without practicing.
- Understand the AP Java Subset and its limitations. (Correct answer)
- Write and debug small Java programs regularly. (Correct answer)
- Solve past FRQs (Free Response Questions). (Correct answer)
Correct answer: Understand the AP Java Subset and its limitations.
Solving past Free Response Questions (FRQs) is an extremely effective way to prepare for the AP CSA exam. It simulates the actual exam experience, allowing students to practice applying their knowledge to complex problems and develop problem-solving strategies. This also helps in understanding the rubric and expectations for earning points on the free-response section.
Question 5: What will the following code output?
- A and B (Correct answer)
- C
- A
- A and C
- B
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 6: What does `str.charAt(0)` return for `str = "Hello"`?
- 'H' (Correct answer)
- 0
- "H"
- 'e'
Correct answer: 'H'
charAt(0) returns the char at index 0, which is 'H' — a primitive char, not a String.
Question 7: What must be true about the argument in each recursive call to guarantee termination?
- It must be larger than the original
- It must move closer to the base case (Correct answer)
- It must be the same value
- It must be a String
Correct answer: It must move closer to the base case
Each recursive call must use an argument that brings it closer to the base case, ensuring eventual termination.
Question 8: What is true about abstract classes in Java?
- An abstract class cannot be instantiated. (Correct answer)
- An abstract class must have at least one abstract method.
- 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)
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: Which of the following statements are true about method overloading and overriding?
- Overridden methods must have the same method signature. (Correct answer)
- Overriding requires inheritance. (Correct answer)
- Overloading occurs within the same class. (Correct answer)
- Overloaded methods must have different method names.
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 10: Which of the following best describes mutual recursion?
- A loop that calls a recursive method
- A method calling itself twice
- A method with two base cases
- 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 11: Which of these correctly initializes a 2D array with 3 rows and 4 columns?
- int[][] grid = new int[4][3];
- int grid[3][4];
- int[][] grid = new int[3][4]; (Correct answer)
- int[3][4] grid = new int[][];
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 12: Which of the following sorts has average-case time complexity of O(n log n)?
- Selection sort
- Bubble sort
- Insertion sort
- Merge sort (Correct answer)
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 13: Which loop is most commonly used to iterate over every element in an array in AP CSA?
- indexed for loop only
- for-each (enhanced for loop) (Correct answer)
- while loop
- do-while loop
Correct answer: for-each (enhanced for loop)
The enhanced for loop (for-each) is idiomatic for iterating all elements when the index is not needed.
Question 14: What is the best-case time complexity of bubble sort when the array is already sorted?
- O(log n)
- O(n) (Correct answer)
- O(1)
- O(n²)
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 15: In the Fibonacci sequence defined recursively as fib(n)=fib(n-1)+fib(n-2), what are the base cases?
- fib(1)=1 only
- fib(0)=1 and fib(1)=1
- 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: What is the sum of all elements in a 2D array `int[][] a = {{1,2},{3,4},{5,6}}`?
- 18
- 15
- 12
- 21 (Correct answer)
Correct answer: 21
Summing all elements: 1+2+3+4+5+6 = 21, using a nested loop to add each element.
Question 17: Which method adds an element to the end of an ArrayList?
- add() (Correct answer)
- push()
- append()
- insert()
Correct answer: add()
The ArrayList method add(element) appends the specified element to the end of the list.
Question 18: Which keyword is used to declare an interface in Java?
- extends
- abstract
- implements
- interface (Correct answer)
Correct answer: interface
The `interface` keyword declares an interface in Java, defining a contract of method signatures without implementations.
Question 19: What does `str.toUpperCase()` do?
- Returns a new String with all characters converted to uppercase (Correct answer)
- Converts only the first character to uppercase
- Throws an exception if numbers are present
- Modifies the original String in place
Correct answer: Returns a new String with all characters converted to uppercase
toUpperCase() returns a new String with all alphabetic characters converted to uppercase; Strings in Java are immutable so the original is unchanged.
Question 20: What is the recursive case?
- The parameter declaration
- The part of the method that makes a call to itself with a smaller/simpler input (Correct answer)
- The method's return statement
- 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 21: What will be the output of this code?
- Wednesday Invalid day (Correct answer)
- Error
- Invalid day
- Wednesday
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 22: What does `list.remove(0)` do to an ArrayList?
- Throws an exception
- Removes the last element
- Removes the element at index 0 (Correct answer)
- Removes all elements equal to 0
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 23: What is the time complexity of linear search in the worst case?
- O(log n)
- O(1)
- O(n²)
- O(n) (Correct answer)
Correct answer: O(n)
Linear search checks every element in the worst case (target not present), giving O(n) time complexity.
Question 24: What does the following code print? abstract class Animal { String name; Animal(String n) { name = n; } abstract String sound(); public String toString() { return name + " says " + sound(); } } class Cat extends Animal { Cat(String n) { super(n); } String sound() { return "meow"; } } // In main: Animal a = new Cat("Whiskers"); System.out.println(a);
- Whiskers says meow (Correct answer)
- A compile-time error
- Whiskers says null
- Animal says meow
Correct answer: Whiskers says meow
The Cat constructor passes "Whiskers" to the Animal constructor via super(n), and the overridden sound() returns "meow", so toString() produces "Whiskers says meow".
Question 25: What happens if a class implements an interface but does not override all of its abstract methods?
- The compiler ignores unimplemented methods
- The class must be declared abstract (Correct answer)
- A runtime exception is thrown
- The class inherits empty implementations
Correct answer: The class must be declared abstract
If a class does not implement all abstract methods of an interface, it must itself be declared abstract; otherwise, the code will not compile.
Question 26: In recursion, each method call gets its own set of local variables stored where?
- In the heap
- In a global variable table
- In a static memory block
- 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 27: What is the output if `System.out.print(n+" ");` is moved AFTER `count(n-1);` in the previous question?
- 3 2 1 0
- 0 1 2
- 3 2 1
- 1 2 3 (Correct answer)
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 28: How many times is the base case reached when computing factorial(3) recursively?
- 3
- 4
- 0
- 1 (Correct answer)
Correct answer: 1
factorial(3) calls factorial(2) which calls factorial(1) which calls factorial(0) — the base case is reached exactly once.
Question 29: How does encapsulation improve code quality?
- By making all class variables public.
- By hiding the implementation details of a class. (Correct answer)
- 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 30: What is the time complexity of a simple linear recursion that calls itself once per step from n down to 0?
- O(log n)
- O(1)
- O(n) (Correct answer)
- O(n²)
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 31: 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 32: What is the purpose of the `@Override` annotation in Java?
- It prevents the method from being overridden further
- It makes the method run faster
- It tells the compiler to verify the method actually overrides a superclass method (Correct answer)
- It marks the method as abstract
Correct answer: It tells the compiler to verify the method actually overrides a superclass method
`@Override` is a compile-time check that causes a compile error if the annotated method doesn't actually override a superclass method.
Question 33: What is the output of the following code?
- Error
- PCSA
- APCSA
- AP
- 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 34: What is the output of the following code?
- Both Some sound and Bark
- Some sound
- Error: Animal cannot refer to Dog.
- Bark (Correct answer)
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 35: Which of the following are primitive data types in Java?
- boolean (Correct answer)
- int (Correct answer)
- char (Correct answer)
- ArrayList
- String
Correct answer: boolean
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 36: What is the output of the following code?
- 0,0 0,1 1,0 1,1
- 0,0 0,1 0,2 1,0 1,1 1,2 (Correct answer)
- 0,0 1,0
- 0,0 0,1 0,2
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 37: What is the difference between method overriding and method overloading?
- Overloading only works in subclasses
- Overriding redefines a superclass method; overloading defines multiple methods with the same name but different parameters (Correct answer)
- Overriding uses different parameter lists; overloading uses the same parameters
- They are the same thing
Correct answer: Overriding redefines a superclass method; overloading defines multiple methods with the same name but different parameters
Overriding replaces a superclass method in a subclass with the same signature, while overloading creates multiple methods with the same name but different parameters in the same class.
Question 38: Given `interface Printable { void print(); }` and `abstract class Document implements Printable { }`, which statement is TRUE about Document?
- Document must implement print() immediately
- Document cannot implement Printable because it is abstract
- Document can leave print() unimplemented since it is abstract (Correct answer)
- Document automatically gets a default print() from Printable
Correct answer: Document can leave print() unimplemented since it is abstract
An abstract class is not required to implement inherited abstract interface methods; it can defer that responsibility to its concrete subclasses.
Question 39: What exception is thrown when you access an array index that is out of bounds?
- ArrayIndexOutOfBoundsException (Correct answer)
- NullPointerException
- IllegalArgumentException
- IndexOutOfRangeException
Correct answer: ArrayIndexOutOfBoundsException
Java throws ArrayIndexOutOfBoundsException when you access a negative index or an index >= array length.
Question 40: What error occurs when a recursive method has no base case or never reaches it?
- StackOverflowError (Correct answer)
- NullPointerException
- ArrayIndexOutOfBoundsException
- RecursionLimitException
Correct answer: StackOverflowError
Infinite recursion causes the call stack to fill up, resulting in a StackOverflowError at runtime.
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