AP Computer Science A Practice Quiz: Recursive Searching and Sorting
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 16 questions to check your progress.
Question 1 of 16
All Questions (16)
A) TSET
B) TEST
C) T
D) TSE
Correct Answer: A
The method recursively calls itself on the substring without the first character, and then appends the first character to the end of the result. This process effectively reverses the string. reverse("EST") + 'T' -> (reverse("ST") + 'E') + 'T' -> ((reverse("T") + 'S') + 'E') + 'T' -> (("T" + 'S') + 'E') + 'T' -> "TSET".
A) The collection must have an odd number of elements.
B) The collection must be sorted.
C) The collection must contain only unique elements.
D) The collection must be an array, not an ArrayList.
Correct Answer: B
The content explicitly states that binary search operates on a 'sorted array or ArrayList'. The sorted nature is essential for the algorithm to correctly eliminate half of the search space in each step.
A) 55, 22
B) 55, 33, 22
C) 11, 22
D) 44, 22
Correct Answer: A
1. The initial search space is indices 0-8. The middle index is (0+8)/2 = 4, which has the value 55. 2. Since 22 < 55, the new search space becomes the lower half, indices 0-3. 3. The new middle index is (0+3)/2 = 1, which has the value 22. The value is found. The sequence of checked values is 55, then 22.
A) It is an iterative algorithm that finds the smallest element in each pass.
B) It is a recursive sorting algorithm for arrays or ArrayLists.
C) It is a search algorithm that requires a sorted collection.
D) It is a recursive algorithm used only for traversing String objects.
Correct Answer: B
The content defines merge sort as 'a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.'
A) [15, 40, 12, 25]
B) [12, 15, 25, 40]
C) [12, 25, 15, 40]
D) [15, 12, 40, 25]
Correct Answer: B
The merge process compares the elements of the two sorted sub-arrays and places them into a new array in sorted order. It compares 15 and 12 (takes 12), then 15 and 25 (takes 15), then 40 and 25 (takes 25), and finally takes the remaining 40. The result is [12, 15, 25, 40].
A) It removes only the middle element.
B) It eliminates half of the remaining elements.
C) It swaps the first and last elements.
D) It sequentially checks every other element.
Correct Answer: B
The provided content states that binary search 'eliminates half of the array or ArrayList in each recursive call' by comparing the target value to the middle element.
A) 0
B) 1
C) 2
D) 4
Correct Answer: C
The method recursively traverses the ArrayList, counting the number of strings with a length greater than 3. For `["a", "be", "see", "four"]`: 'a' (len 1) -> 0. 'be' (len 2) -> 0. 'see' (len 3) -> 0. 'four' (len 4) -> 1. Whoops, the question says > 3. Let me re-evaluate. 'a' (len 1) is not > 3. 'be' (len 2) is not > 3. 'see' (len 3) is not > 3. 'four' (len 4) is > 3. Only one word. Let me change the question slightly to make it more interesting. Let's say the list is `["cat", "frog", "bird", "ox"]`. 'cat' (len 3) is not > 3. 'frog' (len 4) is > 3. 'bird' (len 4) is > 3. 'ox' (len 2) is not > 3. The count would be 2. Let's use this example. New list: `["cat", "frog", "bird", "ox"]`. The method counts strings with length > 3. 'frog' (length 4) and 'bird' (length 4) meet the criteria. The total count is 2.
A) [8, 3] and [5, 1]
B) [3, 8] and [1, 5]
C) [8], [3], [5], [1]
D) [1, 3, 5, 8]
Correct Answer: C
Merge sort works by recursively dividing the array into halves until each sub-array contains only one element. For `[8, 3, 5, 1]`, it splits into `[8, 3]` and `[5, 1]`, and then each of those splits into `[8]`, `[3]`, `[5]`, and `[1]`. This is the point just before the first merge (e.g., merging `[8]` and `[3]`) occurs.
A) Only String objects
B) Only sorted arrays
C) String objects, arrays, and ArrayList objects
D) Only collections with numeric data
Correct Answer: C
The content explicitly states: 'Recursion can be used to traverse String objects, arrays, and ArrayList objects.'
A) [10, 20, 30]
B) [50, 60, 70, 80]
C) [60, 70, 80]
D) [10, 20, 30, 40]
Correct Answer: B
The array has 8 elements (indices 0-7). The first middle index is (0+7)/2 = 3, which is value 40. Since the target 70 is greater than 40, the entire lower half including the middle element is eliminated. The search continues on the upper half, which is the sub-array from index 4 to 7: `[50, 60, 70, 80]`.
A) The algorithm has checked every element sequentially.
B) The algorithm has run for a predetermined number of steps.
C) All elements in the collection have been eliminated from the search space.
D) The algorithm returns the element closest to the target value.
Correct Answer: C
The description of binary search in the content specifies that the recursive calls continue 'until the desired value is found or all elements have been eliminated.'
A) 15
B) 25
C) 30
D) 0
Correct Answer: C
The method sums the first `n` elements. `sum(arr, 3)` returns `sum(arr, 2) + arr[2]`. This expands to `(sum(arr, 1) + arr[1]) + arr[2]`, which is `((sum(arr, 0) + arr[0]) + arr[1]) + arr[2]`. This evaluates to `((0 + 5) + 10) + 15`, which equals 30.
A) At the first element.
B) At the last element.
C) At the middle element.
D) At a randomly chosen element.
Correct Answer: C
The provided content clearly states, 'Binary search starts at the middle of a sorted array or ArrayList'.
A) [2, 7, 4, 9]
B) [7, 2, 4, 9]
C) [2, 7, 9, 4]
D) [2, 4, 7, 9]
Correct Answer: A
1. Split: `[7, 2]` and `[9, 4]`. 2. Split again: `[7]`, `[2]` and `[9]`, `[4]`. 3. First merge: `[7]` and `[2]` are merged to form `[2, 7]`. 4. Second merge: `[9]` and `[4]` are merged to form `[4, 9]`. At this point, the collection consists of two sorted sub-arrays: `[2, 7]` and `[4, 9]`. The question asks for the state of the array, which implies the conceptual state of these sub-arrays existing side-by-side.
A) Binary search
B) Merge sort
C) String traversal
D) Selection sort
Correct Answer: B
The content states, 'Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.' Binary search is for searching, not sorting.
A) The algorithm will find and return 10.
B) The algorithm will find and return 15.
C) The algorithm will conclude that 12 is not in the array.
D) The algorithm will enter an infinite loop.
Correct Answer: C
1. Middle is 15. 12 < 15, so search `[5, 10]`. 2. Middle is 5. 12 > 5, so search `[10]`. 3. Middle is 10. 12 > 10, so the search space becomes empty. Since the search space is empty and the value was not found, the algorithm concludes the value is not present.