AP® Computer Science A Practice Questions
Work through AP Computer Science A practice questions unit by unit, 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 3032
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 3033
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 3034
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 3035
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 3036
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 3037
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 3038
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 3039
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 3040
SurveyResults analyzes survey scores. scores is a nonnull ArrayList, possibly empty, with every value in [0,100]. The constructor keeps the supplied list reference.
public class SurveyResults {
private java.util.ArrayList scores
public SurveyResults(java.util.ArrayList initial){scores=initial;}
// Implement countAbove and trim.
}| Operation | Result |
|---|---|
| Initial scores | [85,42,90,42,73,100,15] |
| countAbove(50) | 4 |
| trim(40,90) | [85,42,90,42,73] |
| then trim(50,80) | [73] |
(a) Implement countAbove(int threshold), returning the number of scores strictly greater than threshold.
(b) Implement trim(int minAccept,int maxAccept), removing scores outside the inclusive range [minAccept,maxAccept], while preserving the relative order of all remaining values. Assume 0 <= minAccept <= maxAccept <= 100. Handle empty lists, removal of every element, and consecutive removals.
Main Solution
Answer Key
(a)
public int countAbove(int threshold){
int count=0
for(int value:scores)if(value>threshold)count++
return count
}
(b)
public void trim(int minAccept,int maxAccept){
for(int i=scores.size()-1;i>=0;i--)
if(scores.get(i)maxAccept)scores.remove(i)
}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.

