AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Sorting And Searching Clear filter
Question 122
Question 123
Question 124
Question 125
Question 126
Question 127
Question 128
After exactly 2 passes of this partial insertion sort, what is the array?
int[] arr = {8, 3, 5, 1, 4};
for (int i = 1; i < 3; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}Main Solution
Step-by-step trace of the code:
Initial array: {8, 3, 5, 1, 4}
Pass 1 (i = 1):
key = arr[1] = 3j = 0- While loop:
j >= 0(true) ANDarr[0] > key→8 > 3(true):arr[1] = arr[0]→ array becomes{8, 8, 5, 1, 4};j = -1 - While loop:
j >= 0(false) → exit arr[j + 1] = arr[0] = key = 3→ array becomes{3, 8, 5, 1, 4}
Pass 2 (i = 2):
key = arr[2] = 5j = 1- While loop:
j >= 0(true) ANDarr[1] > key→8 > 5(true):arr[2] = arr[1]→ array becomes{3, 8, 8, 1, 4};j = 0 - While loop:
j >= 0(true) ANDarr[0] > key→3 > 5(false) → exit arr[j + 1] = arr[1] = key = 5→ array becomes{3, 5, 8, 1, 4}
After exactly 2 passes, the array is {3, 5, 8, 1, 4}, which matches option B.
Teacher Explanation
This question tests students' ability to trace a partial insertion sort. The loop runs only for i = 1 and i = 2 (since i < 3), so only the first three elements get sorted among themselves. The last two elements (indices 3 and 4) remain untouched. After 2 passes, the subarray {8, 3, 5} becomes {3, 5, 8}, giving {3, 5, 8, 1, 4}.
Education Coach Advice
AP Exam Strategy: When tracing sorting algorithms, always track the exact loop bounds first. Here, i < 3 means only passes for i = 1 and i = 2 occur — only indices 0–2 are affected. Write out the array state after each pass to avoid errors. Elements beyond the loop's reach stay in their original positions.
Traps
Common traps:
- Option A ({1, 3, 5, 8, 4}): Students may think the entire array gets sorted or confuse the loop bound
i < 3with more passes, incorrectly including element1in the sorted portion. - Option C/D: Students may miscount the number of passes or misread the loop condition, thinking 4 or 5 passes occur instead of just 2.
Answer images
Question 129
Question 130
int[] a = { 2, 5, 8, 11, 14, 17, 20, 23, 26, 29 };
int target = 20;
int low = 0;
int high = a.length - 1;
int steps = 0;
int foundIdx = -1;
while (low <= high && foundIdx == -1)
{
int mid = (low + high) / 2;
steps++;
if (a[mid] == target)
{
foundIdx = mid;
}
else if (a[mid] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
System.out.println(steps);
What is printed when the code segment executes?Main Solution
Solution:
Answer: A. Trace the four iterations: start low = 0, high = 9, mid = 4, a[4] = 14 < 20, set low = 5; next mid = (5 + 9) / 2 = 7, a[7] = 23 > 20, set high = 6; next mid = (5 + 6) / 2 = 5, a[5] = 17 < 20, set low = 6; next mid = (6 + 6) / 2 = 6, a[6] = 20 — match, foundIdx = 6. Four iterations total.
Answer: Correct: A
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

