Senior interviewers probe performance tuning, concurrency, distributed systems, and real-world edge cases:
Q16. Explain Java 21 Virtual Threads (Project Loom) and how they differ from platform threads.
Experienced • 3-5+ Yrs
Platform threads are 1:1 wrappers over operating system kernel threads, consuming roughly 1MB of stack memory and limited to a few thousand per JVM. Virtual Threads (Java 21) are lightweight user-mode threads managed directly by the JVM runtime, consuming mere bytes of memory. Millions can run concurrently; when a virtual thread blocks on I/O, the JVM unmounts it from its carrier platform thread, maximizing CPU utilization.
Q17. Explain the JVM memory model: Heap (Young/Old Generation), Metaspace, and Stack.
Experienced • 3-5+ Yrs
The JVM memory is split into: Heap (shared memory storing objects, divided into Young Generation [Eden, Survivor S0/S1] for short-lived objects and Old/Tenured Generation for long-lived objects), Metaspace (native memory storing class metadata, replacing PermGen in Java 8), and JVM Stacks (per-thread memory holding stack frames, local primitive variables, and references).
Q18. How does ConcurrentHashMap achieve high concurrency without locking the entire table?
Experienced • 3-5+ Yrs
In Java 8+, ConcurrentHashMap avoids table-wide locking and segment locks. It uses Compare-And-Swap (CAS) instructions for lock-free bucket initialization and synchronization (synchronized block) locked exclusively on the first node of each specific bucket list/tree, allowing parallel concurrent writes across different buckets.
Q19. Explain Java Memory Model (JMM) happens-before relationship and the 'volatile' keyword.
Experienced • 3-5+ Yrs
The JMM defines how threads interact through memory. The 'volatile' keyword guarantees visibility (writes to a volatile variable are immediately flushed to main memory and subsequent reads see the latest value) and prevents instruction reordering by inserting memory barriers, but does not guarantee atomicity for compound operations like count++.
Q20. How do you prevent Deadlocks in multi-threaded Java applications?
Experienced • 3-5+ Yrs
Deadlocks occur when threads hold locks while waiting for locks held by each other in a circular chain. Prevention strategies include: enforcing a strict global lock acquisition hierarchy, using ReentrantLock.tryLock() with timeouts, avoiding nested synchronized blocks, and using higher-level java.util.concurrent utilities.
Q21. Explain the Spring Bean Lifecycle from instantiation to destruction.
Experienced • 3-5+ Yrs
1. Instantiation (constructor call) -> 2. Populate Properties (dependency injection) -> 3. BeanNameAware / BeanFactoryAware -> 4. BeanPostProcessor beforeInitialization -> 5. @PostConstruct / InitializingBean.afterPropertiesSet() / custom init-method -> 6. BeanPostProcessor afterInitialization -> 7. Bean is ready -> 8. PreDestroy / DisposableBean.destroy().
Q22. What is the difference between @Component, @Service, and @Repository in Spring?
Experienced • 3-5+ Yrs
@Component is the generic stereotype for any Spring-managed component. @Service is a specialized stereotype for business logic layer classes. @Repository is for data access layers, providing automatic translation of database platform exceptions into Spring's unified DataAccessException hierarchy.
Q23. How does Spring Boot Auto-Configuration work under the hood?
Experienced • 3-5+ Yrs
Spring Boot scans META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each auto-configuration class evaluates conditional annotations such as @ConditionalOnClass, @ConditionalOnMissingBean, and @ConditionalOnProperty to decide whether to register beans automatically based on classpath dependencies.
Q24. Explain Java 8 Streams: intermediate vs terminal operations.
Experienced • 3-5+ Yrs
Streams process data declaratively. Intermediate operations (filter, map, sorted, flatMap) are lazy and return a new Stream without executing computation until a terminal operation is reached. Terminal operations (collect, forEach, reduce, count) trigger pipeline execution and produce a result or side-effect, closing the stream.
Q25. What is the difference between CompletableFuture and Future in Java?
Experienced • 3-5+ Yrs
Future represents an asynchronous computation but requires blocking via .get() or polling via .isDone() to retrieve results. CompletableFuture (Java 8) supports non-blocking functional chaining (thenApply, thenCompose, thenCombine), exception handling (exceptionally, handle), and manual completion via .complete().
Q26. How do you handle Distributed Transactions across Microservices (Saga Pattern)?
Experienced • 3-5+ Yrs
Distributed 2PC (Two-Phase Commit) introduces high latency and single points of failure. The Saga pattern executes a sequence of local transactions across services. If a step fails, the saga executes compensating transactions in reverse order to rollback state. Sagas can be Choreography-based (event-driven via Kafka) or Orchestration-based (central coordinator).
Q27. Explain ClassLoader hierarchy in Java (Bootstrap, Platform/Extension, Application).
Experienced • 3-5+ Yrs
The JVM uses a delegation model: when a class is requested, a ClassLoader delegates up to its parent before searching itself. Bootstrap ClassLoader loads core JDK runtime classes (rt.jar / java.base). Platform/Extension ClassLoader loads platform extensions. Application/System ClassLoader loads application classes on the user CLASSPATH.
Q28. What are Java Records and Sealed Classes introduced in modern Java?
Experienced • 3-5+ Yrs
Records (Java 16) are concise, immutable data carriers that automatically generate getters, constructor, equals(), hashCode(), and toString(). Sealed Classes (Java 17) restrict which other classes or interfaces may extend or implement them using the 'permits' clause, enabling exhaustive pattern matching.
Q29. How does Spring Security implement JWT authentication in REST APIs?
Experienced • 3-5+ Yrs
A client sends credentials to an authentication endpoint. Upon validation, the server generates a signed JSON Web Token containing claims. Subsequent client requests attach this token in the 'Authorization: Bearer ' header. A custom OncePerRequestFilter intercepts requests, validates the signature, extracts user details, and populates the SecurityContextHolder.
Q30. Explain database connection pool tuning in Spring Boot using HikariCP.
Experienced • 3-5+ Yrs
HikariCP is the default high-performance connection pool in Spring Boot. Tuning involves setting maximumPoolSize (often formula: pool_size = Tn * (core_count * 2) + effective_spindle_count), minimumIdle, connectionTimeout (max wait before throwing SQLException), and idleTimeout to prevent thread starvation and DB socket leaks.