JAVA · HARD CODEX
Java Interview Questions — Hard
Deep runtime internals, memory models, distributed design, concurrency failure modes, and architectural decisions.
50 Theory Questions7 Implementation Folios5 Free Model Answers
THEORY QUESTIONS & SOLUTIONSShowing 5 of 50 questions
Q1Detail the lifecycle of HotSpot JVM thread synchronization locks (Lock Inflation).
- Mark Word: An object's memory header contains a "Mark Word" storing locking state bits.
- Stage 1: Biased Locking (Deprecated/Removed): Thread ID is written into the Mark Word. Subsequent locking attempts by the same thread skip synchronization operations completely.
- Stage 2: Lightweight Locking: If another thread attempts acquisition, the lock inflates to lightweight. The JVM uses CAS to copy the Mark Word to the lock-seeking thread's stack. If it succeeds, the lock is acquired. If contention occurs, the thread spins briefly.
- Stage 3: Heavyweight Locking (Inflated): If contention persists, the lock inflates to heavyweight. The Mark Word is rewritten to point to an OS-level monitor (
ObjectMonitor). Threads are parked and queued in native OS wait states, incurring heavy kernel context-switching penalties.
Q2Compare JVM Garbage Collection algorithms: Serial, Parallel, G1, ZGC, and Shenandoah.
- Serial GC: Single-threaded; pauses all application threads ("Stop-the-World"/STW) for both young and old collections. Ideal for tiny memory footprints.
- Parallel GC: Multi-threaded young and old collection. High throughput, but causes significant STW pauses.
- G1 (Garbage-First) GC: Divides the heap into equal region blocks. It targets regions containing the most garbage first, performing incremental, concurrent collections to meet configurable maximum pause times (
-XX:MaxGCPauseMillis). - ZGC: A scalable, low-latency concurrent collector using colored pointers (storing metadata in reference bits) and load barriers (intercepting references to update object coordinates on-the-fly). STW pauses are sub-millisecond, independent of heap scale (supports terabytes).
- Shenandoah: Similar to ZGC, uses concurrent evacuation barriers to compact the heap concurrently, minimizing pause times.
Q3Explain memory barriers and compilation instruction reordering under the JMM.
- Instruction Reordering: Compilers, JVM, and CPU architectures reorder execution instructions to maximize pipelining and cache efficiency, provided single-thread execution outcomes remain unchanged.
- Memory Barriers (Fences): Hard CPU instructions inserted by the JVM to enforce memory ordering:
LoadLoad: Prevents subsequent reads from reordering before previous reads.StoreStore: Prevents subsequent writes from reordering before previous writes.LoadStore: Prevents subsequent writes from reordering before previous reads.StoreLoad: The strongest fence; forces all previous writes to main memory before subsequent reads are evaluated.
Q4Detail Classloading resolution, initializing sequences, and Class.forName() vs ClassLoader.loadClass().
- Execution order:
- Loading: Reads bytecode binary streams.
- Linking: Verifies bytecode structure, prepares static fields with default values, and optionally resolves symbolic references.
- Initialization: Executes static initializer blocks (
static {}) and assigns real values to static fields.
Class.forName("MyClass"): Loads, links, and initializes the class, running static blocks immediately.ClassLoader.loadClass("MyClass"): Only loads the class; linking and initialization are delayed until the class is instantiated or referenced.
Q5How do you trace and diagnose memory leaks, thread starvation, and CPU spikes?
- Memory Leaks: Generate heap dumps using
jmap -dump:live,format=b,file=heap.hprof [pid]. Analyze references using Eclipse Memory Analyzer (MAT) to identify leak suspects retaining massive heaps (retained size). - Thread Starvation/Deadlocks: Generate thread dumps using
jstack [pid]orjcmd [pid] Thread.print. Examine thread states to locate deadlocks or threads blocked indefinitely on monitor lock acquisitions. - CPU Spikes: Profile CPU hot paths using
async-profileror JProfiler to capture on-cpu stack traces, identifying performance-intensive methods and loop bottlenecks.
Unlock the remaining 52 Java (Hard) questions
You've completed the 5 free sample questions. Get unrestricted lifetime access to every question, model answer, implementation challenge, and all 27+ technologies for a single payment.
₹399 India / $9 International · One-time settlement · Zero subscription