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

  1. 9.1 Recursion
  2. 9.2 Searching
  3. 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

  1. A recursive method is one that

    1. contains a loop
    2. calls itself
    3. returns another method
    4. has no parameters

    Answer: B. calls itself Recursion solves a problem by calling the same method on a smaller instance.

  2. Linear (sequential) search

    1. requires the data to be sorted before it can begin
    2. always checks the middle element first, then halves the range
    3. checks elements one at a time and works on unsorted data
    4. 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.

  3. Selection sort works by

    1. repeatedly inserting the next element into its correct place in the sorted part
    2. splitting the array in half, sorting each half, and merging the sorted halves
    3. repeatedly finding the smallest remaining element and swapping it into the next position
    4. 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.

  4. Every correct recursive method needs

    1. at least two parameters to pass along
    2. a loop that repeats the recursive call
    3. a static variable to count the calls
    4. 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.

  5. Binary search requires that the data be

    1. unsorted
    2. sorted
    3. stored in an ArrayList only
    4. 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.

  6. Insertion sort works by

    1. shifting each next element left into its place among the already-sorted elements
    2. finding the smallest remaining element and swapping it to the front each pass
    3. splitting the array into halves recursively and merging the sorted halves
    4. 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.

  7. What is returned by f(4)? public int f(int n) { if (n == 0) return 0; return n + f(n - 1); }

    1. 4
    2. 10
    3. 24
    4. 0

    Answer: B. 10 4 + 3 + 2 + 1 + 0 = 10.

  8. In the worst case, about how many comparisons does binary search need for a sorted array of 1,000 elements?

    1. 500
    2. 10
    3. 1,000
    4. 100

    Answer: B. 10 Each step halves the range; 2¹⁰ = 1024, so about 10 comparisons.

  9. After the first pass of selection sort on {5, 3, 8, 1, 9}, the array is

    1. {1, 3, 8, 5, 9}
    2. {3, 5, 8, 1, 9}
    3. {1, 5, 3, 8, 9}
    4. {3, 5, 1, 8, 9}

    Answer: A. {1, 3, 8, 5, 9} The smallest element, 1, is swapped with the first element, 5.

  10. What is returned by fact(5)? public int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); }

    1. 15
    2. 25
    3. 60
    4. 120

    Answer: D. 120 5 × 4 × 3 × 2 × 1 = 120.

Practice all 30 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.