Attic
DSAExam Prep & Patterns

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

ExpressionReturn TypeCritical Exam Trap
list.add(e)booleanAppends element; always returns true for standard lists
list.add(index, e)voidIndexed insertion; returns void (not boolean!)
list.get(index)EReturns element at index
list.set(index, e)EReturns the previous element replaced at index
list.remove(int index)ERemoves by index and returns the deleted element
list.remove(Object o)booleanRemoves by value; returns true if found
stack.push(e)ELegacy Stack.push returns the pushed item
deque.push(e)voidModern Deque.push returns void
queue.offer(e)booleanSafe insertion; returns false on full capacity
queue.remove()EThrows NoSuchElementException if empty
queue.poll()EReturns null if empty
queue.element()EThrows NoSuchElementException if empty
queue.peek()EReturns null if empty
map.put(k, v)VReturns previous value mapped to k, or null if new
map.remove(k)VReturns removed value, or null if key was missing
set.add(e)booleanReturns 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 10

Null Tolerance Matrix

StructureNull Elements Allowed?Null Keys Allowed?Null Values Allowed?
Array (Object[])Yes——
ArrayListYes (Multiple)——
LinkedListYes (Multiple)——
StackYes (Multiple)——
ArrayDequeNo (Throws NPE)——
PriorityQueueNo (Throws NPE)——
HashSetYes (Exactly 1)——
LinkedHashSetYes (Exactly 1)——
TreeSetNo (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


Interface Reference vs Runtime Implementation

When declaring variables, compile-time method availability follows the reference type, whereas runtime dispatch executes the concrete implementation:

ReferenceVersusImplementation.java
// 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.

On this page