AP Computer Science A Multiple-Choice Practice Quiz
Practice tracing Java classes, method calls, arrays, and ArrayLists. Later questions cover sorting, recursion, and how loop order changes the result of a two-dimensional array update.
High school students familiar with Java syntax and basic loopsThe Counter class has a private int field named count and a public instance method int readCount() that returns count. From a separate top-level class, which expression reads the value for an existing Counter named counter?
Ready to play?
Open at Step 3 with every question loaded. Play solo, host live, or customize your game.
Play & Customize GameQuestions by ladder level
The Counter class has a private int field named count and a public instance method int readCount() that returns count. From a separate top-level class, which expression reads the value for an existing Counter named counter?
- counter.readCount()
- counter.count
- counter.readCount(1)
- counter.readCount
Show answer and explanation
Correct answer: A. counter.readCount()
The public instance method reads its own object's private field. Call the method with parentheses on the counter object.
A method use(int amount) requires amount to be between 0 and remaining, inclusive. A valid call subtracts amount from remaining. If remaining starts at 6, which call follows the contract and leaves exactly 2?
- use(7)
- use(2)
- use(4)
- use(-2)
Show answer and explanation
Correct answer: C. use(4)
Four falls within the allowed range. Subtracting it from six leaves two.
A Jar starts with start + 1 items when constructed as new Jar(start). Its add(n) method adds n items, and count() returns the current count. Given Jar a = new Jar(2); Jar b = new Jar(4); a.add(b.count() - 1); what are the counts for a and b, in that order?
- 7 and 9
- 7 and 5
- 6 and 5
- 3 and 5
Show answer and explanation
Correct answer: B. 7 and 5
The constructors set the counts to 3 and 5. The call adds 5 - 1 = 4 to a. The count for b stays at 5.
The Token class has a static int created that starts at 0 and an instance field points. Its constructor sets the new token's points and increments created. A program creates a token a with 4 points, then creates a second token with 7 points. It calls a.use(), which subtracts 1 from a's points. What are created and a's points now?
- 2 and 6
- 1 and 3
- 2 and 4
- 2 and 3
Show answer and explanation
Correct answer: D. 2 and 3
Both constructor calls update the shared field created, bringing it to 2. The use call changes only a's points, from 4 to 3.
A meter starts at 6. Each call to meter.next() returns its current value and then advances the stored value by 2 before the call ends. What value does 2 * meter.next() + meter.next() produce?
- 22
- 20
- 18
- 24
Show answer and explanation
Correct answer: B. 20
Java computes the left operand of the addition first. The first call supplies 6 to the multiplication, and the second supplies 8. The sum is 12 + 8 = 20.
Given int[] weights = {2, 4, 7}; the program runs for (int weight : weights) { weight += 1; }. What is the sum of the three elements stored in weights afterward?
- 13
- 16
- 14
- 10
Show answer and explanation
Correct answer: A. 13
Each iteration copies a primitive value into the loop variable. Updating that variable leaves the array elements at 2, 4, and 7, which sum to 13.
Given int[] values = {1, 2, 3, 4}; the program runs int end = values[3]; for (int i = 3; i > 0; i--) { values[i] = values[i - 1]; }. It then assigns values[0] = end. What does values contain afterward?
- [1, 1, 2, 3]
- [2, 3, 4, 1]
- [4, 1, 1, 1]
- [4, 1, 2, 3]
Show answer and explanation
Correct answer: D. [4, 1, 2, 3]
The loop copies from right to left, shifting each of the first three elements one place right before its old slot is overwritten. The saved 4 moves into index 0.
An ArrayList<String> named labels contains [sun, rain, fog]. The program calls labels.add(1, "wind") and then labels.set(2, "snow"). What is the final list?
- [sun, wind, snow, rain, fog]
- [sun, wind, snow]
- [sun, wind, snow, fog]
- [sun, snow, rain, fog]
Show answer and explanation
Correct answer: C. [sun, wind, snow, fog]
Inserting wind at index 1 shifts rain and fog to the right. The set call replaces rain at index 2 with snow and keeps the size at 4.
An ArrayList<Integer> named values starts as [3, 5, 2, 9]. The program runs int total = 0; while (values.size() > 2) { total += values.remove(1); }. What are total and values afterward?
- 7 and [3, 9]
- 14 and [3, 2]
- 2 and [3, 9]
- 8 and [2, 9]
Show answer and explanation
Correct answer: A. 7 and [3, 9]
The first removal returns 5 and shifts 2 into index 1. The second removal returns 2. Their sum is 7, and the two remaining elements are 3 and 9.
Given int[][] table = {{3, 1, 4}, {1, 5, 9}}; the program runs int total = 0; for (int row = 0; row < table.length; row++) { total += table[row][1]; }. What is total afterward?
- 23
- 4
- 13
- 6
Show answer and explanation
Correct answer: D. 6
The row index changes while the column stays at index 1. This adds the second entry of each row, 1 and 5.
Given int[][] tags = {{2, 4, 6}, {1, 3, 5}}; an outer loop visits columns from left to right. For each column, an inner loop visits both rows from top to bottom. Starting with int code = 0, each visit runs code = 10 * code + tags[row][col]. What is the final value?
- 123456
- 246135
- 214365
- 246
Show answer and explanation
Correct answer: C. 214365
The visits read 2, 1, 4, 3, 6, and 5. Multiplying the current value by 10 before each addition appends that entry as a digit.
An ascending selection sort makes each pass by swapping the smallest element of the unsorted suffix into that suffix's first slot. Starting with [7, 5, 2, 9, 3], what does the array contain immediately after two passes?
- [2, 3, 5, 7, 9]
- [2, 3, 7, 9, 5]
- [2, 3, 5, 9, 7]
- [2, 5, 7, 9, 3]
Show answer and explanation
Correct answer: B. [2, 3, 7, 9, 5]
The first pass swaps 2 with 7. The second finds 3 in the remaining suffix and swaps it with 5, producing [2, 3, 7, 9, 5].
What does total(6) return for this Java method? int total(int n) { if (n <= 1) { return n; } return n + total(n - 2); }
- 13
- 21
- 10
- 12
Show answer and explanation
Correct answer: D. 12
The arguments are 6, 4, 2, and 0. The call with 0 reaches the base case, so the returns add up to 6 + 4 + 2 + 0 = 12.
A Shelf starts with private int[] counts = {1, 3}. Its move(int amount) method returns false immediately if amount <= 0 || counts[0] < amount. Otherwise it subtracts amount from counts[0], adds it to counts[1], and returns true. After calls to move(2) and then move(1), what were the two returns and what does counts contain?
- false, true, with [0, 4]
- false, false, with [1, 3]
- true, false, with [-1, 5]
- false, true, with [0, 3]
Show answer and explanation
Correct answer: A. false, true, with [0, 4]
The first call returns false before changing either count because only one item is available. The next call moves that item to the second slot and returns true.
A rectangular int[][] grid starts as [[1, 0, 0, 1], [0, 1, 0, 0]]. For each row r, the inner loop runs for (int c = grid[r].length - 1; c >= 1; c--) { if (grid[r][c - 1] == 1) { grid[r][c] = 1; } }. What does grid contain afterward?
- [[1, 0, 0, 1], [0, 1, 0, 0]]
- [[1, 1, 1, 1], [0, 1, 1, 1]]
- [[1, 1, 0, 1], [0, 1, 1, 0]]
- [[1, 1, 0, 0], [0, 0, 1, 0]]
Show answer and explanation
Correct answer: C. [[1, 1, 0, 1], [0, 1, 1, 0]]
Right-to-left order checks each neighbor before that neighbor can change. The original 1s stay in place and each marks one slot to its right when that slot exists.