Traps & MCQ Drills
Return-type trap overloads, null tolerance matrix across all structures, and interactive True/False questions
This reference details the high-frequency traps and edge cases that appear in technical interviews, online assessments, and multiple-choice questions (MCQs).
Return-Type Overload Traps
| Expression | Return Type | Critical Exam Trap |
|---|---|---|
list.add(e) | boolean | Appends element; always returns true for standard lists |
list.add(index, e) | void | Indexed insertion; returns void (not boolean!) |
list.get(index) | E | Returns element at index |
list.set(index, e) | E | Returns the previous element replaced at index |
list.remove(int index) | E | Removes by index and returns the deleted element |
list.remove(Object o) | boolean | Removes by value; returns true if found |
stack.push(e) | E | Legacy Stack.push returns the pushed item |
deque.push(e) | void | Modern Deque.push returns void |
queue.offer(e) | boolean | Safe insertion; returns false on full capacity |
queue.remove() | E | Throws NoSuchElementException if empty |
queue.poll() | E | Returns null if empty |
queue.element() | E | Throws NoSuchElementException if empty |
queue.peek() | E | Returns null if empty |
map.put(k, v) | V | Returns previous value mapped to k, or null if new |
map.remove(k) | V | Returns removed value, or null if key was missing |
set.add(e) | boolean | Returns false if element was already present |
The list.remove() Integer Overload Trap:
List<Integer> list = new ArrayList<>(List.of(10, 20, 30));
list.remove(1); // Calls list.remove(int index) -> Removes element at index 1 (value 20)
list.remove(Integer.valueOf(10)); // Calls list.remove(Object) -> Removes value 10Null Tolerance Matrix
| Structure | Null Elements Allowed? | Null Keys Allowed? | Null Values Allowed? |
|---|---|---|---|
Array (Object[]) | Yes | — | — |
ArrayList | Yes (Multiple) | — | — |
LinkedList | Yes (Multiple) | — | — |
Stack | Yes (Multiple) | — | — |
ArrayDeque | No (Throws NPE) | — | — |
PriorityQueue | No (Throws NPE) | — | — |
HashSet | Yes (Exactly 1) | — | — |
LinkedHashSet | Yes (Exactly 1) | — | — |
TreeSet | No (Throws NPE) | — | — |
HashMap | — | Yes (Exactly 1) | Yes (Multiple) |
LinkedHashMap | — | Yes (Exactly 1) | Yes (Multiple) |
TreeMap | — | No (Throws NPE) | Yes (Multiple) |
ConcurrentHashMap | — | No (Throws NPE) | No (Throws NPE) |
List.of() / Set.of() / Map.of() | No (Throws NPE) | No (Throws NPE) | No (Throws NPE) |
High-Frequency MCQ Statements
False. Queue<E> is an interface in java.util. It is implemented by classes like ArrayDeque, LinkedList, and PriorityQueue.
False. Map<K, V> is a completely distinct root interface. It does not extend Collection or Iterable.
False. Accessing list.get(index) in a LinkedList requires traversing node pointers from
the head or tail, taking O(n) linear time.
False. HashSet makes no iteration-order guarantees. To preserve insertion order, use
LinkedHashSet.
False. A for-each loop traverses the backing binary array directly. Only the root is
guaranteed to be the minimum/maximum. Elements must be extracted via poll() to observe priority
order.
False. A map can contain at most one null key (stored in bucket 0). Adding another mapping
with a null key overwrites the existing value.
False. It returns a fixed-size wrapper backed by the original array. Calling .add() or
.remove() throws UnsupportedOperationException.
False. It pre-allocates an internal capacity of 100, but .size() is 0. Attempting to read
index 0 throws IndexOutOfBoundsException.
False. TreeSet implements Set<E>, meaning duplicate values are rejected (set.add(x) returns false).
False. Keys must be unique. Adding an existing key updates its value and returns the previous value.
False. Multiple distinct keys can associate with the identical value object.
True. ArrayDeque is faster and avoids the synchronized lock contention of the legacy Stack
class.
False. Array uses a public final field a.length without parentheses. Collections use the
method c.size().
False. It resolves to list.remove(int index), removing the element at index 1! To remove value 1, pass an object: list.remove(Integer.valueOf(1)).
Interface Reference vs Runtime Implementation
When declaring variables, compile-time method availability follows the reference type, whereas runtime dispatch executes the concrete implementation:
// Compile-time type is List<Integer>
List<Integer> list = new ArrayList<>();
list.add(10);
list.get(0);
// ❌ COMPILE ERROR: ensureCapacity() is not defined on List<E>!
// list.ensureCapacity(50);
// ✅ Must be declared as ArrayList, or cast:
((ArrayList<Integer>) list).ensureCapacity(50);MCQ Rule of Thumb: The compiler verifies syntax against the left-hand reference type. The JVM executes behavior according to the right-hand runtime object.