Q1
Why can double-checked locking fail in Java without the volatile keyword on the singleton reference?
Without volatile, the JVM/CPU can reorder object allocation, reference assignment, and constructor initialization. Thread B may see a non-null reference whose constructor fields are still default-zeroed, causing sporadic null/corrupt state crashes.
Q2
Why does CompletableFuture.supplyAsync() without an explicit Executor pose a risk in server applications?
By default it uses ForkJoinPool.commonPool(), which shares CPU-sized worker threads across the entire JVM. Blocking I/O inside commonPool() starves unrelated parallel streams and framework tasks across the whole application.
Q3
What happens in Java NIO / JDBC if a PreparedStatement batch inserts 100k rows inside a single transaction without periodic flushes?
The JDBC driver buffers batch parameters and DB undo/redo logs grow unbounded, causing OutOfMemoryError on the JVM client or transaction log exhaustion on the server. Chunk batches (e.g., every 1,000–5,000 rows) and commit/clear explicitly.
Q4
Why does HashMap convert a bucket linked list into a balanced red-black tree at threshold 8, and revert at 6?
Converting at 8 (TREEIFY_THRESHOLD) turns worst-case O(n) hash-collision lookups into O(log n). Leaving hysteresis down to 6 (UNTREEIFY_THRESHOLD) avoids expensive repeated treeify/untreeify thrashing when keys bounce around size 8.
Q5
How can ReentrantLock.lockInterruptibly() prevent permanent thread deadlocks that synchronized cannot?
A thread blocked waiting to enter a synchronized monitor cannot respond to Thread.interrupt(). lockInterruptibly() lets a waiting thread abort cleanly on timeout or cancellation signal, enabling deadlock recovery.
Q6
Why might JDBC ResultSet with TYPE_SCROLL_INSENSITIVE degrade memory on large exports?
Scrollable drivers cache fetched rows in client or server memory to support backwards navigation (previous(), last()). For large sequential exports, always use TYPE_FORWARD_ONLY with CONCUR_READ_ONLY and an explicit setFetchSize().
Q7
In multithreaded Java, why is AtomicInteger faster under low-to-medium contention than synchronized, and when does LongAdder beat both?
AtomicInteger uses hardware CAS loops without OS thread blocking, whereas LongAdder stripes increments across a dynamic cell array per thread—dramatically reducing cache-line bouncing under heavy write contention.
Q8
What subtle serialization vulnerability arises if a class implements Serializable without validating invariants in readObject()?
Deserialization bypasses constructors entirely and reconstructs raw byte state. Attackers can forge illegal field combinations or gadget chains unless defensive validation is enforced in readObject() or deserialization filters (ObjectInputFilter) are configured.
Q9
Why does Tomcat/Servlet filter order behave symmetrically on request and response?
Servlet filters wrap chain.doFilter(req, res) like a call stack. Code before doFilter runs top-down on the inbound request, while code after doFilter unwinds bottom-up on the outbound response—crucial for timing, compression, and security headers.
Q10
What happens when a daemon thread holds a file handle or DB connection during JVM shutdown?
The JVM terminates immediately once all non-daemon user threads finish—WITHOUT running finally blocks inside daemon threads—potentially leaving half-written files or unclosed external locks.