AP® Computer Science A Data Collections Multiple Choice Questions
Work through AP Computer Science A multiple-choice 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.
Question 1012
arr is an int array.
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6};
int count = 0;
for (int i = 0; i < arr.length - 1; i++)
{
if (arr[i] < arr[i + 1])
{
count++;
}
}
System.out.println(count);
What is printed?Main Solution
Solution:
Correct: B. Compare each adjacent pair: 3<1 F, 1<4 T, 4<1 F, 1<5 T, 5<9 T, 9<2 F, 2<6 T. That is 4 true comparisons.
Answer: Correct: B
Answer images
Question 1013
String whose length is less than 3 from list.
public static void cleanup(ArrayList<String> list)
{
for (int i = 0; i < list.size(); i++)
{
if (list.get(i).length() < 3)
{
list.remove(i);
}
}
}
Given the initial list ["a", "bb", "ccc", "dd", "eeee", "ff", "gg"], what does list contain after a call to cleanup(list)?Main Solution
Solution:
Correct: D. Classic index-skipping bug: after removing, i still increments, skipping the element that shifts into position i. Trace: i=0 "a" remove → [bb,ccc,dd,eeee,ff,gg]; i=1 "ccc" keep; i=2 "dd" remove → [bb,ccc,eeee,ff,gg]; i=3 "ff" remove → [bb,ccc,eeee,gg]; i=4, size=4 so loop ends. Final: ["bb","ccc","eeee","gg"].
Answer: Correct: D
Answer images
Question 1014
int[][] m =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
What is printed by the following code segment?
int total = 0;
for (int r = 0; r < m.length; r++)
{
total += m[r][m[r].length - 1 - r];
}
System.out.println(total);Main Solution
Solution:
Correct: D. This computes the anti-diagonal sum. r=0: m[0][3]=4. r=1: m[1][2]=7. r=2: m[2][1]=10. r=3: m[3][0]=13. Total = 4 + 7 + 10 + 13 = 34.
Answer: Correct: D
Answer images
Question 1015
public static int mystery(int n)
{
if (n <= 1)
{
return n;
}
return n + mystery(n / 2);
}
What value is returned by the call mystery(20)?Main Solution
Solution:
Correct: B. Trace: mystery(20)=20+mystery(10); mystery(10)=10+mystery(5); mystery(5)=5+mystery(2); mystery(2)=2+mystery(1); mystery(1)=1 (base). Unwind: 2+1=3; 5+3=8; 10+8=18; 20+18=38.
Answer: Correct: B
Answer images
Question 1016
int array.
int[] arr = {2, 5, 1, 8, 3, 9, 4};
int best = arr[0] + arr[1];
for (int i = 1; i < arr.length - 1; i++)
{
int s = arr[i] + arr[i + 1];
if (s > best)
{
best = s;
}
}
System.out.println(best);
What is printed?Main Solution
Solution:
Correct: C. Adjacent pair sums: (2+5)=7, (5+1)=6, (1+8)=9, (8+3)=11, (3+9)=12, (9+4)=13. Initial best=7, updated successively to 9, 11, 12, 13. Maximum is 13.
Answer: Correct: C
Answer images
Question 1017
int[][] g =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int r = g.length - 1; r > 0; r--)
{
g[r][0] = g[r - 1][0];
}
for (int r = 0; r < g.length; r++)
{
System.out.print(g[r][0] + " ");
}
What is printed?Main Solution
Solution:
Correct: D. The loop shifts column 0 downward (propagating upstream values). Starting column 0 = [1, 4, 7]. r=2: g[2][0]=g[1][0]=4 → column 0 = [1, 4, 4]. r=1: g[1][0]=g[0][0]=1 → column 0 = [1, 1, 4]. Output: 1 1 4.
Answer: Correct: D
Answer images
Question 1018
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++)
{
a.add(i * 3);
}
for (int i = 0; i < a.size(); i++)
{
if (i % 2 == 0)
{
a.set(i, a.get(i) + i);
}
}
System.out.println(a);
What is printed?Main Solution
Solution:
Correct: A. First loop fills [3, 6, 9, 12, 15, 18]. Second loop modifies only even indices by adding the index: i=0→3+0=3, i=2→9+2=11, i=4→15+4=19. Odd indices unchanged. Result: [3, 6, 11, 12, 19, 18].
Answer: Correct: A
Answer images
Question 1019
public static int digitCount(int n)
{
if (n < 10)
{
return 1;
}
return 1 + digitCount(n / 10);
}
What value is returned by the call digitCount(84263)?Main Solution
Solution:
Correct: C. Each recursive call strips the last digit (integer division by 10). digitCount(84263)=1+digitCount(8426)=2+digitCount(842)=3+digitCount(84)=4+digitCount(8)=4+1=5.
Answer: Correct: C
Answer images
Question 1020
int[][] grid = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int sum = 0;
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
if (r == c || r + c == grid[0].length - 1)
{
sum = sum + grid[r][c];
}
}
}
What value is stored in sum after the code segment executes?Main Solution
Solution:
Answer: C. The condition admits cells on the main diagonal (r == c) and on the anti-diagonal (r + c == cols - 1 == 3). Main diagonal: grid[0][0] = 1, grid[1][1] = 6, grid[2][2] = 11 — sum 18. Anti-diagonal: grid[0][3] = 4, grid[1][2] = 7, grid[2][1] = 10 — sum 21. Because the grid is not square (3×4), the two diagonals share no cell, so double counting cannot occur. Total 18 + 21 = 39.
Answer: Correct: C
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.

