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 loops

Question 1 of 15

$100

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?

Ready to play?

Open at Step 3 with every question loaded. Play solo, host live, or customize your game.

Play & Customize Game

Questions by ladder level

Level 1 · Class access

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?

  1. counter.readCount()
  2. counter.count
  3. counter.readCount(1)
  4. 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.

Question reference

Level 2 · Method contracts

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?

  1. use(7)
  2. use(2)
  3. use(4)
  4. use(-2)
Show answer and explanation

Correct answer: C. use(4)

Four falls within the allowed range. Subtracting it from six leaves two.

Question reference

Level 3 · Constructors and independent objects

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?

  1. 7 and 9
  2. 7 and 5
  3. 6 and 5
  4. 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.

Question reference

Level 4 · Class and instance state

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?

  1. 2 and 6
  2. 1 and 3
  3. 2 and 4
  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.

Question reference

Level 5 · Method evaluation order

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?

  1. 22
  2. 20
  3. 18
  4. 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.

Question reference

Level 6 · Enhanced for loops

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?

  1. 13
  2. 16
  3. 14
  4. 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.

Question reference

Level 7 · Array rotation

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, 1, 2, 3]
  2. [2, 3, 4, 1]
  3. [4, 1, 1, 1]
  4. [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.

Question reference

Level 8 · ArrayList updates

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?

  1. [sun, wind, snow, rain, fog]
  2. [sun, wind, snow]
  3. [sun, wind, snow, fog]
  4. [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.

Question reference

Level 9 · ArrayList removal and traversal

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?

  1. 7 and [3, 9]
  2. 14 and [3, 2]
  3. 2 and [3, 9]
  4. 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.

Question reference

Level 10 · Summing a column

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?

  1. 23
  2. 4
  3. 13
  4. 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.

Question reference

Level 11 · Column-major traversal

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?

  1. 123456
  2. 246135
  3. 214365
  4. 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.

Question reference

Level 12 · Selection sort

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?

  1. [2, 3, 5, 7, 9]
  2. [2, 3, 7, 9, 5]
  3. [2, 3, 5, 9, 7]
  4. [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].

Question reference

Level 13 · Recursive calls

What does total(6) return for this Java method? int total(int n) { if (n <= 1) { return n; } return n + total(n - 2); }

  1. 13
  2. 21
  3. 10
  4. 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.

Question reference

Level 14 · Method results and state changes

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?

  1. false, true, with [0, 4]
  2. false, false, with [1, 3]
  3. true, false, with [-1, 5]
  4. 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.

Question reference

Level 15 · In-place two-dimensional updates

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. [[1, 0, 0, 1], [0, 1, 0, 0]]
  2. [[1, 1, 1, 1], [0, 1, 1, 1]]
  3. [[1, 1, 0, 1], [0, 1, 1, 0]]
  4. [[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.

Question reference