Java Interview Guide — Part A & B
This document contains Part A (Basics: REST, Spring MVC, Java
fundamentals) and Part B (Java 8, 11, 17, 21, 25 feature highlights).
It is structured for senior Java developers preparing for
backend/microservices interviews.
Part A — Basics (REST, Spring MVC, Java
Fundamentals)
1. Explain the REST request flow end-to-end (Client →
Server → Client)
High-level flow:
1. Client sends an HTTP request (method, URL, headers, payload).
2. Server (Spring Boot embedded server—Tomcat/Jetty/Netty)
receives the request.
3. Request enters the Servlet container and is forwarded to
DispatcherServlet — the Spring MVC front controller.
4. DispatcherServlet uses Handler Mappings to locate the correct
@Controller / @RestController method.
5. Handler Adapters invoke the controller method.
6. Argument Resolvers populate controller parameters (@PathVariable,
@RequestBody, @RequestParam, etc.).
7. Controller executes business logic and returns a value.
8. HttpMessageConverters serialize return objects → JSON/XML based
on Accept header.
9. Response flows back through filters, interceptors, security layers.
10. Client receives HTTP response.
Key components:
Filter chain: Authentication, logging, tracing.
Interceptors: Pre/post controller logic.
ExceptionResolver: Handles exceptions → error JSON.
2. @RestController and related annotations — how they work
internally
What is @RestController?
@RestController = @Controller + @ResponseBody
Meaning: - Spring will NOT use a view resolver. - Return values are
automatically serialized to JSON/XML.
Common mapping annotations:
@RequestMapping — base mapping for class or methods.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping,
@PatchMapping — convenient method-specific shortcuts.
@PathVariable — binds URI variables.
@RequestParam — binds query parameters.
@RequestBody — uses HttpMessageConverters to map request JSON →
POJO.
@ResponseStatus — return fixed HTTP status code.
Exception handling annotations:
@ExceptionHandler — controller-level exception handling.
@ControllerAdvice — global exception handling.
How Spring resolves arguments:
HandlerMethodArgumentResolver handles things like: - JSON → object
deserialization - principal injection - default values
How return values work:
HandlerMethodReturnValueHandler processes return values: - serializes
objects - handles ResponseEntity - handles asynchronous return types
(CompletableFuture, Mono/Flux)
3. Singleton classes — creation, best practices, and
breaking singletons
Classic Singleton patterns:
1. Eager initialization:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() { return INSTANCE; }
}
2. Lazy initialization + double-checked locking:
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized ([Link]) {
if (instance == null) instance = new Singleton();
}
}
return instance;
}
}
3. Initialization-on-demand Holder idiom:
public class Singleton {
private Singleton() {}
private static class Holder {
static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() { return [Link]; }
}
4. Enum Singleton — safest:
public enum Singleton {
INSTANCE;
}
How singletons can be broken:
1. Reflection:
Accessing private constructor. Fix: Add guard in constructor or use enum.
2. Serialization:
Deserialization creates a new instance. Fix: implement readResolve().
3. Cloning:
If clone() is allowed. Fix: override clone to throw an exception.
4. Multiple classloaders:
Each classloader creates its own class instance. Fix: design deployment to
avoid duplicate loaders.
4. Thread safety and concurrency fundamentals
Key primitives:
synchronized, ReentrantLock
volatile
Atomic classes (AtomicInteger, AtomicReference, …)
Immutable objects
ConcurrentHashMap
ForkJoinPool, CompletableFuture
Best practices:
Favor immutability.
Avoid shared mutable state.
Use thread pools for scalable concurrency.
Part B — Java 8, 11, 17, 21, 25 Features
Java 8 — Most important features
Lambdas — functional programming style.
Stream API — map/filter/reduce pipelines.
Functional interfaces (Predicate, Function, Supplier).
Default methods in interfaces.
Optional<T> — avoids null checks.
New Date/Time API ([Link]).
Java 11 — Key enterprise-relevant features
LTS release — widely used in enterprise.
New HttpClient API (fully replacing legacy HttpURLConnection).
String additions: isBlank, strip, lines, repeat.
Optional::isEmpty.
Removed Java EE modules from JDK (JAX-WS, JAXB).
Local variable type inference via var (actually introduced in Java
10).
Java 17 — LTS + modern language changes
Sealed classes — control inheritance.
Records — concise immutable data carriers.
Pattern matching for instanceof.
Switch expression refinements.
Performance improvements (G1/ ZGC enhancements).
Java 21 — Major LTS with Project Loom
1. Virtual Threads
Lightweight, high-concurrency threads (~millions possible).
Remove need for thread pools.
Great fit for microservices.
2. Structured Concurrency
Manage groups of tasks as a unit.
3. Pattern Matching Enhancements
For switch, for records.
4. Sequenced Collections
Consistent ordering APIs for lists, sets.
Java 25 — Newest JDK (highlights)
Scoped/Stable Values — safer thread-local-like state.
Pattern Matching for primitives — more expressive conditions.
Vector API — SIMD operations.
Improved JFR profiling — better latency diagnostics.
Module import declarations — ease of modularization.
End of Part A & B Document
You can request additional parts (C, D, Scenario Questions, System Design,
AWS, etc.) to be generated as separate documents.