Mixed Deck — All AP CSA Topics Flashcards
100 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 20 Mixed Deck — All AP CSA Topics flashcards as text
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);
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".
What precondition must be met before applying binary search to an array?
Answer: The array must be sorted
Binary search requires the array to be sorted so it can correctly discard half the remaining elements at each step.
What is the purpose of the `@Override` annotation in Java?
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.
Which loop is most commonly used to iterate over every element in an array in AP CSA?
Answer: for-each (enhanced for loop)
The enhanced for loop (for-each) is idiomatic for iterating all elements when the index is not needed.
Which OOP principle allows a subclass object to be used wherever a superclass reference is expected?
Answer: Polymorphism
Polymorphism allows a subclass instance to be assigned to a superclass reference variable, enabling flexible and interchangeable use.
What is the length of an array declared as `int[] arr = new int[5];`?
Answer: 5
The integer passed to `new int[5]` specifies the array length as 5 elements (indices 0–4).
How many total method calls does factorial(4) make (including the initial call)?
Answer: 5
factorial(4) calls factorial(3), which calls factorial(2), factorial(1), and factorial(0) — 5 calls total including the first.
What keyword makes a String variable refer to a string literal shared in the string pool?
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.
What does `Collections.sort(list)` require of the objects in the list?
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.
What is the index of the first element in a Java array?
Answer: 0
Java arrays are zero-indexed, so the first element is always at index 0.
Can an abstract class contain non-abstract (concrete) methods?
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.
Which of the following strategies is most effective when debugging a program?
Answer: Insert System.out.println() statements at key points.
Inserting `System.out.println()` statements at key points is a highly effective debugging strategy. It allows programmers to inspect the values of variables and trace the program's execution flow. This provides crucial insights into where and why a program might be behaving unexpectedly, helping to pinpoint logical errors.
Which of the following correctly accesses the last element of an array `arr` of length n?
Answer: arr[n-1]
Since arrays are zero-indexed, the last element is at index n-1 where n is the array's length.
What is the purpose of `str.trim()` in Java?
Answer: Returns a new String with leading and trailing whitespace removed
trim() returns a copy of the String with whitespace (spaces, tabs, newlines) stripped from both the beginning and end.
By default, all methods declared inside an interface are:
Answer: public and abstract
Interface methods are implicitly public and abstract, meaning any implementing class must provide a concrete implementation.
In the AP CSA exam, what is the standard way to search for a value in an unsorted ArrayList?
Answer: Sequential (linear) search
An unsorted ArrayList requires sequential search since binary search requires sorted data, and ArrayList has no built-in hash lookup.
Which of the following best describes the purpose of an interface in Java?
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.
In AP CSA, when a method parameter is declared with an interface type, what objects can be passed to it?
Answer: Any object whose class implements that interface
Because of polymorphism, any object whose class implements the specified interface can be passed to a method that accepts an interface-type parameter.
What does `str.contains("ell")` return for `str = "Hello"`?
Answer: true
contains() returns true if the specified sequence appears anywhere within the String; "ell" is found within "Hello".
When encountering a challenging question on the AP CSA multiple-choice section, what should you do?
Answer: Skip it an return later.
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.