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: Arrays Clear filter
Question 282
Question 283
Question 284
Question 285
Question 286
Question 287
What is the content of arr after executing the following?
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int n = arr.length;
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n / 2 + i];
arr[n / 2 + i] = temp;
}Main Solution
The correct answer is C: {5, 6, 7, 8, 1, 2, 3, 4}.
Let's trace through the code step by step.
Initial array: {1, 2, 3, 4, 5, 6, 7, 8}, indices 0–7.n = 8, so n / 2 = 4.
The loop runs for i = 0, 1, 2, 3 (while i < 4). Each iteration swaps arr[i] with arr[4 + i]:
- i = 0: swap
arr[0](1) andarr[4](5) → array becomes{5, 2, 3, 4, 1, 6, 7, 8} - i = 1: swap
arr[1](2) andarr[5](6) → array becomes{5, 6, 3, 4, 1, 2, 7, 8} - i = 2: swap
arr[2](3) andarr[6](7) → array becomes{5, 6, 7, 4, 1, 2, 3, 8} - i = 3: swap
arr[3](4) andarr[7](8) → array becomes{5, 6, 7, 8, 1, 2, 3, 4}
The algorithm swaps the first half of the array with the second half, resulting in {5, 6, 7, 8, 1, 2, 3, 4}.
Teacher Explanation
This question tests array manipulation and loop tracing. The loop swaps each element in the first half of the array with its corresponding element in the second half (offset by n/2). The result is that the two halves are exchanged. Students must carefully trace the index arithmetic: arr[i] pairs with arr[n/2 + i] for each i from 0 to n/2 - 1.
Education Coach Advice
When tracing array loops on the AP exam, write out the array state after each iteration. Pay close attention to index expressions like n / 2 + i — integer division is key. A quick table with columns for i, arr[i], and arr[n/2+i] before and after each swap will prevent errors and save time.
Traps
Option A ({8, 7, 6, 5, 4, 3, 2, 1}) is a common trap for students who think the loop reverses the entire array. A full reversal would require swapping arr[i] with arr[n-1-i], not arr[n/2+i].
Option D ({4, 3, 2, 1, 8, 7, 6, 5}) might attract students who think each half is reversed independently.
Option B (unchanged array) would be the result if the swap logic were incorrect (e.g., swapping an element with itself), but the indices here are distinct.
Answer images
Question 288
What is printed?
int[] data = {3, 7, 2, 8, 5, 1, 9, 4, 6};
int maxDiff = 0;
for (int i = 0; i < data.length - 1; i++) {
int diff = Math.abs(data[i] - data[i + 1]);
if (diff > maxDiff) {
maxDiff = diff;
}
}
System.out.println(maxDiff);Main Solution
The code iterates through consecutive pairs in the array {3, 7, 2, 8, 5, 1, 9, 4, 6} and tracks the maximum absolute difference between adjacent elements.
Let's compute each adjacent difference:
| i | data[i] | data[i+1] | |diff| | maxDiff |
|---|---|---|---|---|
| 0 | 3 | 7 | 4 | 4 |
| 1 | 7 | 2 | 5 | 5 |
| 2 | 2 | 8 | 6 | 6 |
| 3 | 8 | 5 | 3 | 6 |
| 4 | 5 | 1 | 4 | 6 |
| 5 | 1 | 9 | 8 | 8 |
| 6 | 9 | 4 | 5 | 8 |
| 7 | 4 | 6 | 2 | 8 |
The maximum absolute difference between any two adjacent elements is 8 (between data[5] = 1 and data[6] = 9). Therefore, System.out.println(maxDiff) prints 8.
The correct answer is C.
Teacher Explanation
This question tests students' ability to trace through a loop that computes the maximum absolute difference between consecutive array elements. The key insight is that the loop runs from index 0 to data.length - 2 (inclusive), comparing each element with the next. Students must correctly apply Math.abs() and track how maxDiff updates. The largest jump occurs between 1 and 9, giving a difference of 8.
Education Coach Advice
When tracing loops that track a running maximum, always complete the full trace — don't stop early. Create a small table with columns for i, data[i], data[i+1], diff, and maxDiff to avoid missing the true maximum. Also, remember that Math.abs() ensures the difference is always non-negative, so direction doesn't matter here.
Traps
Option A (6): A student might stop tracing early (after index 2, where |2−8|=6) and incorrectly conclude the answer is 6. This is a common off-by-one or early-exit tracing error.
Option B (9): A student might confuse the maximum value in the array (9) with the maximum difference, or mistakenly compute a non-adjacent pair difference.
Option D (5): A student might find the second-largest adjacent difference (|7−2|=5 or |9−4|=5) and stop there.
Answer images
Question 289
After one pass of this code, what is arr[arr.length - 1]?
int[] arr = {2, 5, 1, 8, 3};
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}Main Solution
This code performs one pass of a bubble sort over the array {2, 5, 1, 8, 3}. Let's trace through each iteration:
- i = 0: Compare
arr[0]=2andarr[1]=5. 2 < 5, no swap. Array:{2, 5, 1, 8, 3} - i = 1: Compare
arr[1]=5andarr[2]=1. 5 > 1, swap. Array:{2, 1, 5, 8, 3} - i = 2: Compare
arr[2]=5andarr[3]=8. 5 < 8, no swap. Array:{2, 1, 5, 8, 3} - i = 3: Compare
arr[3]=8andarr[4]=3. 8 > 3, swap. Array:{2, 1, 5, 3, 8}
After one full pass, arr[arr.length - 1] = arr[4] = 8. This is a key property of bubble sort: after one pass, the largest element "bubbles up" to the last position.
The correct answer is C) 8.
Teacher Explanation
This question tests students' understanding of one pass of bubble sort. The key insight is that after one complete pass through the array, the maximum element is guaranteed to be in the last position. Students must trace through all four comparisons carefully, noting that only two swaps occur (at i=1 and i=3), and that the value 8 ends up at index 4.
Education Coach Advice
AP Exam Strategy: When tracing array manipulation code, write out the array state after each iteration. For bubble sort questions specifically, remember that one pass always moves the largest unseen element to its correct final position. This saves time and prevents errors from trying to hold the entire state in your head.
Traps
Common traps:
- Option A (3): Students might think the last element stays as the original last element (3), forgetting that swaps can move it.
- Option B (5): Students might stop tracing early or miscalculate which swaps occur.
- Option D (2): Students might confuse the first and last elements or misread the loop direction.
The most appealing wrong answer is likely A (3), since 3 is the original last element and students may not realize it gets swapped with 8 during the last iteration.
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.

