1. ArithmeticEasy
The upload progress bar sits at 0% until the upload finishes. What fixes it?
int percent = done / total * 100;
done / total * 100 → done * 100 / totalint percent → double percentdone / total * 100 → (done / total) * 100.0done / total → done % total
Show answer
Answer: A. done / total * 100 → done * 100 / total
With ints, done / total is 0 until done equals total. Multiply first so the division happens last, or divide as doubles: 100.0 * done / total.
3. Control flowEasy
Paid orders are being shipped and refunded.
- switch (status) {
- case PAID:
- ship(order);
- case CANCELLED:
- refund(order);
- break;
- default:
- hold(order);
- }
Which line has the bug?
Show answer
Answer: Line 3: ship(order);
Without a break after ship(order), execution falls through into the CANCELLED case. Add break;, or use arrow cases (case PAID -> ship(order);, Java 14+), which never fall through.
5. OverflowEasy
Total revenue in cents goes negative on big days. Change one word to fix it.
int totalCents = 0;
for (Order o : orders) totalCents += o.cents();
int → long+= → =for → while- Add
final
Show answer
Answer: A. int → long
An int tops out around $21.4 million in cents and then wraps negative. A long holds about 9.2 × 10¹⁸. For money, also consider Math.addExact to fail loudly instead of wrapping.
6. CollectionsEasy
You need a map whose keys come out in sorted order. Which do you pick, and what does it cost?
- HashMap: O(1) operations, sorted keys
- TreeMap: sorted keys, O(log n) operations
- LinkedHashMap: sorted keys, O(1) operations
- ArrayList of pairs: O(1) everything
Show answer
Answer: B. TreeMap: sorted keys, O(log n) operations
TreeMap is a red-black tree: keys stay sorted and you get firstKey, floorKey, range views and so on, at O(log n) per operation. HashMap is O(1) average with no order.
10. StringsEasy
Each email in the batch repeats the greetings of everyone before it. Why?
StringBuilder greeting = new StringBuilder();
for (User u : users) {
greeting.append("Hi ").append(u.name());
send(u, greeting.toString());
}
- One StringBuilder is shared by every iteration and never cleared
toString() returns a live view that keeps changing after send runsappend returns a new builder, so the old text is kept twice- The for-each loop reuses the previous
u, so names pile up
Show answer
Answer: A. One StringBuilder is shared by every iteration and never cleared
The builder is created once, so every append adds to the text from earlier users. Create it inside the loop, or call greeting.setLength(0) at the start of each iteration.
11. ArithmeticEasy
With 10 tasks and 3 workers, task 10 is never run. Why?
int perWorker = tasks.size() / workers;
for (int w = 0; w < workers; w++) {
assign(w, tasks.subList(w * perWorker, (w + 1) * perWorker));
}
- 10 / 3 is 3 in int math, so only 9 tasks are handed out
subList excludes its end index, so each worker loses one task- The loop needs
w <= workers so it reaches the last worker tasks.size() counts from zero, so it reports 9 tasks
Show answer
Answer: A. 10 / 3 is 3 in int math, so only 9 tasks are handed out
Integer division drops the remainder, so the leftover task is never assigned. Give the remainder to the last worker (end at tasks.size()), or spread it with (w * n) / workers boundaries.
12. StringsEasy
The coupon check throws NullPointerException for customers with no code. What fixes it?
if (couponCode.equals("SAVE10")) applyDiscount(cart);
couponCode.equals("SAVE10") → "SAVE10".equals(couponCode)couponCode.equals("SAVE10") → couponCode == "SAVE10"couponCode.equals("SAVE10") → couponCode.equalsIgnoreCase("SAVE10")"SAVE10" → "SAVE10".intern()
Show answer
Answer: A. couponCode.equals("SAVE10") → "SAVE10".equals(couponCode)
Calling .equals on the constant is null-safe: it just returns false. Objects.equals(couponCode, "SAVE10") works too.
14. CollectionsMedium
A loop removes an item from the list it’s iterating over. What happens?
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
try {
for (Integer n : list) {
if (n == 1) list.remove(n);
}
System.out.println(list);
} catch (ConcurrentModificationException e) {
System.out.println("CME");
}
What does it output?
- [2, 3]
- CME
- [1, 2, 3]
- [1, 3]
Show answer
Answer: B. CME
The for-each loop uses an iterator, and modifying the list directly while iterating makes the iterator fail on its next step. Use iterator.remove() or list.removeIf(n -> n == 1).
15. ResourcesMedium
A few hours after deploy, every request hangs waiting for a database connection. What's the cause?
Connection c = dataSource.getConnection();
PreparedStatement s = c.prepareStatement(SQL);
ResultSet r = s.executeQuery();
return mapRows(r);
- The database is too slow under the new deploy's query load
- The connection is never closed, so the pool slowly runs out
PreparedStatement can only be executed once per connectionmapRows blocks the request thread while it reads rows
Show answer
Answer: B. The connection is never closed, so the pool slowly runs out
Each request borrows a connection and never returns it. Use try-with-resources (try (Connection c = ...; PreparedStatement s = ...; ResultSet r = ...)) so they close even when an exception is thrown.