Data collections practice test
Up to 15 AP Computer Science A questions on data collections, timed at the exam's pace and scored, new every time. Try 3 below first.
Data collections is 30%–40% of the AP Computer Science A exam's multiple choice. Learn it first in the data collections chapter.
3 free data collections practice questions
Real questions from the Atlas AP Computer Science A exam pool, with the answer and the working.
Consider the following code segment.
int[] a = {3, 1, 4, 2}; for (int i = 1; i < a.length; i++) { a[i] += a[i - 1]; } for (int v : a) { System.out.print(v + " "); }What is printed as a result of executing the code segment?
- A3 4 5 6
- B3 4 8 10
- C3 4 8 2
- D10 7 6 2
Show the answer
B.
Going left to right, a[i − 1] already holds the total of everything before it, so each a[i] becomes the running total: 3 4 8 10.
3 4 5 6: adds the original left neighbor: a[i − 1] has already been updated, so it holds a running total.3 4 8 2: leaves the last element: the test isi < a.length.10 7 6 2: totals from the right.Consider the following code segment.
int[] a = {4, 4, 1, 1, 1, 3, 3, 2}; int best = 1; int run = 1; for (int i = 1; i < a.length; i++) { if (a[i] == a[i - 1]) { run++; if (run > best) { best = run; } } else { run = 1; } } System.out.println(best);What is printed as a result of executing the code segment?
- A1
- B2
- C3
- D4
Show the answer
C.
run counts the length of the current block of equal neighbors and best keeps the largest seen. The longest block of equal values has 3 elements.
1: reports the run at the end of the array.2: counts the equal neighbors in the run, not its elements: run starts at 1.4: counts every position equal to its left neighbor: run is reset to 1 when a run ends.Consider the following code segment.
int[] a = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < a.length / 2 + 1; i++) { int temp = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = temp; } for (int v : a) { System.out.print(v + " "); }What is printed as a result of executing the code segment?
- A6 5 3 4 2 1
- B6 5 4 3 2 1
- C1 2 3 4 5 6
- D6 5 4 4 5 6
Show the answer
A.
a.length / 2 + 1 is 4. The first 3 passes reverse the array; the extra pass, i = 3, swaps a[3] with a[2] again, putting the middle two back: 6 5 3 4 2 1.
6 5 4 3 2 1: assumes a full reversal: the extra pass (i = 3) swaps the middle two back.1 2 3 4 5 6: thinks every pair is swapped twice, as it would be withi < a.length.6 5 4 4 5 6: copies without the temporary variable, so values are lost.
Questions people ask
- What is in the AP Computer Science A Data collections practice test?
- Up to 15 multiple-choice questions from this unit, plus a free-response question when the unit has one, timed at the real exam's pace and scored as a percent. The questions are new every time, drawn from a pool of checked questions.
- How much of the AP Computer Science A exam is Data collections?
- 30% to 40% of the multiple-choice questions, by the College Board’s course and exam description.
- Is it free?
- The 3 questions on this page need no account. With a free Atlas account your first unit test is free; Atlas Premium has no limit.