AP® Computer Science A Class Creation Practice Questions
Work through AP Computer Science A practice questions on Class Creation, 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.
Subtopic: Writing Classes Clear filter
Question 252
Question 253
Question 254
Question 255
Question 256
Question 257
Consider the following class and client code.
public class Tank {
private int level;
private int capacity;
public Tank(int cap) { capacity = cap; level = 0; }
public int fill(int amount) {
int space = capacity - level;
if (amount <= space) { level += amount; return 0; }
else { level = capacity; return amount - space; }
}
public int drain(int amount) {
if (amount <= level) { level -= amount; return amount; }
else { int drained = level; level = 0; return drained; }
}
public int getLevel() { return level; }
}
// Client:
Tank t1 = new Tank(50);
Tank t2 = new Tank(30);
int overflow = t1.fill(45);
overflow += t1.fill(20);
int got = t1.drain(overflow);
t2.fill(got);
System.out.println(t1.getLevel() + " " + t2.getLevel());What is printed?
Main Solution
Step-by-step trace:
Setup:
t1 = new Tank(50)→ t1.level = 0, t1.capacity = 50t2 = new Tank(30)→ t2.level = 0, t2.capacity = 30
Line: int overflow = t1.fill(45);
- space = 50 - 0 = 50
- 45 <= 50, so level += 45 → t1.level = 45; return 0
- overflow = 0
Line: overflow += t1.fill(20);
- space = 50 - 45 = 5
- 20 > 5, so level = 50 (capacity); return 20 - 5 = 15
- t1.level = 50; overflow = 0 + 15 = 15
Line: int got = t1.drain(overflow); (overflow = 15)
- 15 <= 50, so level -= 15 → t1.level = 35; return 15
- got = 15
Line: t2.fill(got); (got = 15)
- space = 30 - 0 = 30
- 15 <= 30, so level += 15 → t2.level = 15; return 0
Line: System.out.println(t1.getLevel() + " " + t2.getLevel());
- t1.getLevel() = 35, t2.getLevel() = 15
- Prints: 35 15
The correct answer is B.
Teacher Explanation
This question tests students' ability to trace through object method calls that modify instance variables and return values. Key skills: tracking level state across multiple fill and drain calls, correctly computing overflow (amount - space when overfilling), and understanding that drain returns the amount actually drained. Students must carefully accumulate overflow across two fill calls before draining.
Education Coach Advice
AP Exam Strategy: For code-tracing questions, create a small table tracking each variable after every line. Here: track t1.level, t2.level, overflow, and got. Pay special attention to methods that both modify state AND return a value — both effects matter. Don't skip the += accumulation step for overflow.
Traps
Common traps:
- Option A (45 0): Students may forget that
overflowaccumulates across bothfillcalls, or may not realize the secondfill(20)causes overflow. They might thinkoverflowstays 0 andgot = 0, so t2 gets nothing. - Option C (50 15): Students may correctly compute t2.level = 15 but forget that
drain(15)reduces t1.level from 50 to 35, leaving t1 at 50. - Option D (35 30): Students may think
got = 30(confusing it with t2's capacity) and fill t2 to capacity.
Answer images
Question 258
Consider the following class and client code.
public class Inventory {
private ArrayList<Integer> quantities;
public Inventory() { quantities = new ArrayList<Integer>(); }
public void addItem(int qty) { quantities.add(qty); }
public int removeSmall(int threshold) {
int count = 0;
for (int i = quantities.size() - 1; i >= 0; i--) {
if (quantities.get(i) < threshold) {
quantities.remove(i);
count++;
}
}
return count;
}
public int getSize() { return quantities.size(); }
}
// Client:
Inventory inv = new Inventory();
inv.addItem(5); inv.addItem(12); inv.addItem(3);
inv.addItem(8); inv.addItem(1);
int removed = inv.removeSmall(5);
System.out.println(removed + " " + inv.getSize());What is printed?
Main Solution
Step 1: Build the ArrayList.
After the five addItem calls, quantities contains (index → value):
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 5 | 12 | 3 | 8 | 1 |
Step 2: Trace removeSmall(5). The loop iterates from i = 4 down to i = 0, removing any element strictly less than 5.
- i = 4: value = 1 < 5 → remove index 4. List: [5, 12, 3, 8]. count = 1.
- i = 3: value = 8, 8 < 5? No. No change.
- i = 2: value = 3 < 5 → remove index 2. List: [5, 12, 8]. count = 2.
- i = 1: value = 12, 12 < 5? No. No change.
- i = 0: value = 5, 5 < 5? No (strict less-than). No change.
Step 3: Results. removed = 2. inv.getSize() returns quantities.size() = 3.
Output: 2 3 → Answer D.
Teacher Explanation
This question tests ArrayList traversal from back to front (to safely remove elements without skipping indices), the strict < operator (5 is NOT removed because 5 < 5 is false), and the ability to track state across multiple removals. Students must carefully simulate the loop iteration by iteration.
Education Coach Advice
When tracing ArrayList removal loops on the AP exam, always simulate the list state after each removal. Pay close attention to whether the comparison is < (strict) or <= — boundary values like 5 in this problem are intentional traps. Iterating from the back is the standard safe-removal pattern; recognize it so you don't worry about index skipping.
Traps
Option A (2 5): A student might correctly count 2 removals but forget that the list shrinks, reporting the original size of 5. Option B (3 2): A student might use <= instead of <, also removing the element 5, yielding 3 removals and 2 remaining. Option C (0 3): A student might think the loop iterates forward and skips elements after a removal, or misread the condition entirely.
Answer images
Question 259
Consider the following class and client code.
public class Sequence {
private int current;
private int step;
public Sequence(int start, int s) { current = start; step = s; }
public int next() { current += step; return current; }
public int skip(int n) {
for (int i = 0; i < n; i++) { next(); }
return current;
}
}
// Client:
Sequence seq = new Sequence(10, 3);
int a = seq.next();
int b = seq.skip(4);
int c = seq.next();
System.out.println(a + " " + b + " " + c);
What is printed?
Main Solution
Step-by-step trace:
The Sequence object is created with start = 10 and step = 3, so current = 10.
int a = seq.next();
next()doescurrent += step→current = 10 + 3 = 13, then returnscurrent.
Soa = 13. State:current = 13.int b = seq.skip(4);
skip(4)callsnext()four times:- i=0:
current = 13 + 3 = 16 - i=1:
current = 16 + 3 = 19 - i=2:
current = 19 + 3 = 22 - i=3:
current = 22 + 3 = 25
current = 25. Sob = 25. State:current = 25.- i=0:
int c = seq.next();
next():current = 25 + 3 = 28, returns28.
Soc = 28. State:current = 28.
Output: 13 25 28 → Answer A.
Teacher Explanation
This question tests students' ability to trace mutable object state across multiple method calls. The key insight is that next() modifies current before returning it, and skip(n) calls next() n times internally, advancing the state by n * step. Students must track current carefully through each call.
Education Coach Advice
AP Exam Strategy: When tracing object state, write down the value of each instance variable after every method call. For next()-style methods, note whether the update happens before or after the return. Here, current += step happens first, so the returned value is always the new current. For skip(n), count the loop iterations carefully — the loop runs exactly n times (i = 0 to n-1).
Traps
Common traps:
- Option B (13 22 25): Students may think
skip(4)returns the value after only 3 additional steps (forgetting thatnext()was already called once fora), or miscounting the loop iterations. They might computebas10 + 4*3 = 22, ignoring thatcurrentwas already 13 beforeskip. - Option D (13 25 31): Students may think
skip(4)callsnext()five times instead of four, or add an extra step somewhere. - Option C (10 22 25): Students may think
next()returns the value before incrementing (i.e., returns oldcurrent), givinga = 10.
Answer images
Question 260
Consider the following class and client code.
public class Account {
private int balance;
public Account(int b) { balance = b; }
public int transfer(Account to, int amount) {
int actual = amount;
if (amount > balance) {
actual = balance;
}
balance -= actual;
to.balance += actual;
return actual;
}
public int getBalance() { return balance; }
}
// Client:
Account a = new Account(100);
Account b = new Account(50);
Account c = new Account(20);
int t1 = a.transfer(b, 70);
int t2 = b.transfer(c, 200);
int t3 = c.transfer(a, 40);
System.out.println(a.getBalance() + " " + b.getBalance() + " " + c.getBalance());What is printed?
Main Solution
Step-by-step trace:
Initial state: a.balance = 100, b.balance = 50, c.balance = 20
t1 = a.transfer(b, 70):
- amount = 70, a.balance = 100
- 70 > 100? No → actual = 70
- a.balance -= 70 → a.balance = 30
- b.balance += 70 → b.balance = 120
- t1 = 70
State after t1: a = 30, b = 120, c = 20
t2 = b.transfer(c, 200):
- amount = 200, b.balance = 120
- 200 > 120? Yes → actual = 120
- b.balance -= 120 → b.balance = 0
- c.balance += 120 → c.balance = 140
- t2 = 120
State after t2: a = 30, b = 0, c = 140
t3 = c.transfer(a, 40):
- amount = 40, c.balance = 140
- 40 > 140? No → actual = 40
- c.balance -= 40 → c.balance = 100
- a.balance += 40 → a.balance = 70
- t3 = 40
State after t3: a = 70, b = 0, c = 100
Output: 70 0 100 → Answer D
Teacher Explanation
This question tests students' ability to trace object state mutations across multiple method calls. The key insight is that transfer caps the transfer amount at the sender's current balance (the if (amount > balance) guard), and that each call modifies the balance fields of two Account objects. Students must track the running balance of each account through all three calls in sequence.
Education Coach Advice
AP Exam Strategy: When tracing code with multiple method calls that mutate object state, create a small table tracking each variable's value after each statement. For this type of question, write out: a=?, b=?, c=? after each transfer call. Pay special attention to methods that cap values (like the if (amount > balance) guard here) — these are often the key to getting the right answer and are commonly overlooked.
Traps
Common traps:
- Option A (30 120 20): A student might correctly compute t1 but then forget that t2 and t3 also modify balances, stopping after the first transfer.
- Option C (70 120 100): A student might correctly compute a's and c's final balances but forget that b's balance was fully drained by t2 (because 200 > 120, so actual = 120, leaving b at 0).
- Option B (100 50 20): A student might think the original values are unchanged, perhaps misreading the method as returning a new Account rather than mutating existing ones.
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.

