These are the questions that actually decide Java Developer interviews in India — practice answering out loud before your next round.
Q1.How do Virtual Threads in Java 21 (Project Loom) differ from OS-managed Platform Threads, and what causes thread carrier pinning?
Traditional Platform Threads map 1:1 to operating system kernel threads, each consuming ~1MB of stack memory, limiting concurrency to thousands before exhausting OS memory or context-switching capacity. Java 21 Virtual Threads are lightweight user-mode threads managed by the JVM, consuming only hundreds of bytes of heap memory, allowing millions of concurrent threads. The JVM schedules virtual threads onto a pool of carrier platform threads (ForkJoinPool). When a virtual thread executes blocking I/O, the JVM unmounts its continuation from the carrier thread, freeing the carrier to execute other virtual threads. Thread carrier pinning occurs when a virtual thread blocks inside a synchronized block or native method (JNI), preventing unmounting; resolve by replacing synchronized blocks with ReentrantLock.
Q2.Explain Spring Boot 3's integration with GraalVM Native Images and Ahead-Of-Time (AOT) compilation versus standard JIT compilation.
Standard Spring Boot compiles to bytecode and runs on the HotSpot JVM, where Just-In-Time (JIT) compilation progressively identifies hotspots and optimizes code at runtime, incurring high memory footprint (JVM heap + metaspace) and slower startup times (5–15 seconds). Spring Boot 3 integrates GraalVM Native Image support via Spring AOT. Ahead-Of-Time processing analyzes the application context at build time, eliminates unused code (dead code elimination), pre-computes reflection, proxy configurations, and resource registrations into static C++ machine code. The resulting standalone native executable starts in under 50 milliseconds and consumes up to 80% less memory, making it ideal for serverless and Kubernetes autoscaling.
Q3.How do you identify and resolve the Hibernate/JPA N+1 select problem using @EntityGraph, JOIN FETCH, and batch fetching?
The N+1 problem occurs when querying 1 parent entity (1 SQL query) with a lazy-loaded child collection causes Hibernate to fire N additional individual queries when accessing the children of each parent row. Resolve using: (1) JOIN FETCH in JPQL: SELECT p FROM Parent p JOIN FETCH p.children forces an inner/left join fetching all data in a single SQL query. (2) @EntityGraph: define @EntityGraph(attributePaths = {'children'}) on repository methods, allowing dynamic fetch plan specification without altering query semantics. (3) Batch Fetching: add @BatchSize(size = 50) on the collection mapping or default_batch_fetch_size in properties, directing Hibernate to fetch lazy collections in batches using WHERE parent_id IN (?, ?, ...) reducing N queries to N/50.
Q4.Explain the Java Memory Model (JMM), the happens-before relationship, and the internal guarantees provided by volatile and Atomic variables.
The Java Memory Model defines how threads interact through shared memory and hardware CPU caches. Under JMM, compilers and CPUs can reorder instructions for performance. The happens-before relationship guarantees that memory writes by one statement are visibly observable to another specific statement. volatile provides two critical guarantees: (1) Visibility: reads and writes bypass local CPU registers/caches and go directly to main memory. (2) Ordering: inserts hardware memory barriers (load/store fences) preventing instruction reordering across the volatile variable. However, volatile does not guarantee atomicity for compound operations (e.g. count++). For lock-free atomic compound operations, java.util.concurrent.atomic classes (AtomicInteger, AtomicReference) leverage hardware-level Compare-And-Swap (CAS) instructions.
Q5.How does the Generational Z Garbage Collector (Generational ZGC) achieve sub-millisecond pause times compared to G1GC?
G1GC divides the heap into regions and performs incremental stop-the-world compaction passes, where pause times scale with heap size and live object counts (typically 10ms to 200ms pauses). Generational ZGC (introduced in Java 21) performs virtually all garbage collection work concurrently with application threads, including concurrent marking, concurrent evacuation (copying live objects), and concurrent reference updating. It leverages colored pointers (encoding GC metadata in unused pointer bits) and load barriers (intercepting application pointer dereferences to relocate objects on the fly). Pauses are limited to tiny thread-stack handshakes (< 1 millisecond), independent of heap size (scaling from 16MB to 16TB).
Q6.Explain Spring @Transactional propagation behaviors: contrast REQUIRED vs REQUIRES_NEW and explain why self-invocation bypasses transactions.
Propagation determines transaction boundaries: (1) REQUIRED (default): joins the existing active transaction if one exists; creates a new physical transaction if none exists. If any inner method fails and rolls back, the entire shared transaction is marked rollback-only. (2) REQUIRES_NEW: always pauses the current active transaction and initiates an independent, distinct physical transaction with its own connection. If the inner transaction fails or commits, it does not directly trigger rollback on the outer transaction (unless the exception escapes unhandled). Self-invocation (calling a transactional method from another method within the exact same Spring bean) bypasses transactions because Spring @Transactional relies on AOP dynamic proxies: internal method calls execute on the raw 'this' instance rather than the proxy wrapper.
Q7.How do CompletableFuture and ForkJoinPool handle asynchronous execution, and how do you prevent thread starvation in custom thread pools?
CompletableFuture enables non-blocking functional composition of asynchronous tasks using methods like thenApply, thenCompose, and allOf. By default, CompletableFuture tasks without an explicit Executor run on the shared ForkJoinPool.commonPool(). Because the common pool is shared across parallel streams and system operations with a thread count equal to Runtime.getRuntime().availableProcessors() - 1, running blocking I/O tasks on it causes thread starvation, grinding background application throughput to a halt. Always supply a dedicated, bounded ThreadPoolTaskExecutor with custom corePoolSize, maxPoolSize, and ArrayBlockingQueue tailored specifically to the workload.
Q8.How do you implement distributed tracing and idempotency across event-driven microservices using Spring Cloud Sleuth/Micrometer and Kafka headers?
Distributed tracing propagates traceId (unique to the end-to-end request lifecycle) and spanId (unique to each individual service unit of work) across network boundaries. Micrometer Tracing automatically injects W3C traceparent headers into outbound HTTP calls and Kafka record headers, allowing aggregators like Zipkin or OpenTelemetry/Jaeger to reconstruct the full distributed execution timeline. For event-driven idempotency: each Kafka producer attaches a unique message_id or business idempotency key (e.g. order_id) in the header. The consuming service executes an atomic database check-and-insert into a processed_messages table within the consumer's local database transaction before executing business logic, discarding duplicate arrivals.