Q1
Why does Integer a = 127; Integer b = 127; a == b return true, while 128 == 128 returns false?
Java caches boxed Integer objects in IntegerCache for values -128 to 127 by default. Thus 127 reuses the same cached reference, whereas 128 autoboxes two distinct heap objects, making == compare references instead of numeric values.
Q2
Why can modifying an object's field after inserting it into a HashSet make the element impossible to remove or find?
HashSet stores entries in buckets indexed by hashCode() at insertion time. Mutating a field included in hashCode() changes the runtime hash, so future contains() or remove() calls check the wrong bucket while the object stays orphaned in the old bucket—causing a subtle memory leak.
Q3
What happens if a finally block executes a return statement while an exception was thrown inside try?
A return inside finally silently suppresses and discards both the try return value and any unhandled exception thrown inside try or catch—making debugging nearly impossible. Never put return statements inside finally.
Q4
Why is String safe for HashMap keys and multithreading without synchronization, while StringBuilder is not?
String is immutable with a final backing byte/char array and caches its hashCode() once computed. Because no thread can mutate its contents after creation, references can be shared safely without locks and bucket hashes never drift.
Q5
What is the subtle trap with Arrays.asList(intArray) when passing a primitive int[]?
Arrays.asList expects T.... Because primitives cannot bind to generics, Java treats the entire int[] array as a single element (List<int[]>) of size 1 instead of List<Integer>. Box the array (Integer[]) or use Arrays.stream(intArray).boxed().toList().
Q6
Why does ConcurrentModificationException sometimes NOT trigger when removing the second-to-last element of an ArrayList in a for-each loop?
The ArrayList iterator checks hasNext() (cursor != size) before calling next(). Removing the second-to-last element shifts size down to match cursor, so hasNext() returns false and the loop exits cleanly without reaching checkForComodification()—hiding an intermittent bug.
Q7
How can a Java enum singleton still be broken if you aren't careful—and why is it safer against Reflection/Serialization than double-checked locking?
Unlike class-based singletons, the JVM blocks Constructor.newInstance() on enum types against reflection attacks and handles enum serialization by name identity without invoking readResolve(). Using an enum is the cleanest singleton pattern in Java.
Q8
What happens to the Java heap if a static ThreadLocal is used inside a servlet thread pool without calling .remove()?
Each worker thread holds a ThreadLocalMap whose entries use WeakReference keys, NOT weak values. While pooled threads live indefinitely, values remain strongly reachable even after the request ends—causing severe classloader/heap memory leaks.
Q9
Why does method overloading resolve at compile time while method overriding resolves at runtime?
Overload selection depends on the declared reference types of arguments (javac embeds the exact method descriptor in bytecode). Overriding (invokevirtual) inspects the actual runtime object class in the JVM vtable when executing.
Q10
Why can try-with-resources still report suppressed exceptions you wouldn't see with a plain try/finally?
If both the try body and close() throw exceptions, try-with-resources preserves the primary try exception and attaches the close() exception via Throwable.addSuppressed(), readable with getSuppressed(), whereas a manual finally close overwrites and hides the original root failure.