AP CSA Unit 9: Recursion, Searching, and Sorting
30 multiple-choice questions on AP Computer Science A Unit 9, Recursion, Searching, and Sorting, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 8 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.
Take the practice test · Flashcards · All study tools
What it covers
- 9.1 Recursion
- 9.2 Searching
- 9.3 Sorting
Key terms
9.3 Sorting
- Recursion
- A method that calls itself on a smaller version of the problem
- Base case
- The condition in a recursive method that returns without another recursive call
- StackOverflowError
- Error from too many nested method calls, usually infinite recursion
- Linear search
- Checks each element in order until the target is found
- Binary search
- Repeatedly checks the middle of a sorted range and discards half
- Selection sort
- Repeatedly swaps the smallest remaining element into place
- Insertion sort
- Inserts each element into the sorted part on its left
- Merge sort
- Recursively sorts halves and merges them
Sample practice questions
A recursive method is one that
- contains a loop
- calls itself
- returns another method
- has no parameters
Answer: B. calls itself Recursion solves a problem by calling the same method on a smaller instance.
Linear (sequential) search
- requires the data to be sorted before it can begin
- always checks the middle element first, then halves the range
- checks elements one at a time and works on unsorted data
- only works on 2D arrays, checking each row in turn
Answer: C. checks elements one at a time and works on unsorted data Linear search examines each element in order until the target is found or the list ends.
Selection sort works by
- repeatedly inserting the next element into its correct place in the sorted part
- splitting the array in half, sorting each half, and merging the sorted halves
- repeatedly finding the smallest remaining element and swapping it into the next position
- repeatedly swapping adjacent elements that are out of order until none remain
Answer: C. repeatedly finding the smallest remaining element and swapping it into the next position After pass k, the first k elements are the k smallest, in order.
Every correct recursive method needs
- at least two parameters to pass along
- a loop that repeats the recursive call
- a static variable to count the calls
- a base case that stops the recursion
Answer: D. a base case that stops the recursion Without a base case, the calls never end and the program runs out of stack memory.
Binary search requires that the data be
- unsorted
- sorted
- stored in an ArrayList only
- made of Strings
Answer: B. sorted Binary search eliminates half of the remaining elements each step, which only works if the data are in order.
Insertion sort works by
- shifting each next element left into its place among the already-sorted elements
- finding the smallest remaining element and swapping it to the front each pass
- splitting the array into halves recursively and merging the sorted halves
- repeatedly swapping adjacent out-of-order elements until the array is sorted
Answer: A. shifting each next element left into its place among the already-sorted elements The left part of the array is always sorted, and each new element is inserted into it.
What is returned by f(4)? public int f(int n) { if (n == 0) return 0; return n + f(n - 1); }
- 4
- 10
- 24
- 0
Answer: B. 10 4 + 3 + 2 + 1 + 0 = 10.
In the worst case, about how many comparisons does binary search need for a sorted array of 1,000 elements?
- 500
- 10
- 1,000
- 100
Answer: B. 10 Each step halves the range; 2¹⁰ = 1024, so about 10 comparisons.
After the first pass of selection sort on {5, 3, 8, 1, 9}, the array is
- {1, 3, 8, 5, 9}
- {3, 5, 8, 1, 9}
- {1, 5, 3, 8, 9}
- {3, 5, 1, 8, 9}
Answer: A. {1, 3, 8, 5, 9} The smallest element, 1, is swapped with the first element, 5.
What is returned by fact(5)? public int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); }
- 15
- 25
- 60
- 120
Answer: D. 120 5 × 4 × 3 × 2 × 1 = 120.
More AP Computer Science A
- Unit 1: Primitive Types, Variables, and Expressions
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 3: Boolean Expressions and if Statements
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 6: Arrays
- Unit 7: ArrayList
- Unit 8: 2D Arrays
Studying for a test in your own class? Make a practice test from your class notes.