Interview Q&A · questions with model answers
Java Developer Interview Questions
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked Explain Java concept: JVM.
The JVM is the runtime that loads Java bytecode, verifies it, manages memory, and executes code through interpretation and JIT compilation.
-
Most asked Explain Java concept: hashCode contract.
Objects that are equal according to equals must return the same hashCode, or hash-based collections become incorrect.
-
Most asked Explain Java concept: String immutability.
Java String is immutable: once created, its character content cannot be changed.
-
Most asked Java Collections: HashMap?
HashMap stores key-value pairs using hashCode to choose a bucket and equals to find the matching key.
-
Most asked Java Collections: ConcurrentHashMap?
ConcurrentHashMap is a thread-safe hash map optimized for concurrent access without synchronizing the entire map for every operation.
-
Most asked Concurrency in Java: synchronized?
synchronized provides mutual exclusion and memory visibility using an object's monitor lock.
-
Most asked Describe the request flow in a Spring MVC (Spring Boot) application.
The servlet container runs the filter chain (including Spring Security), then the DispatcherServlet finds a handler via HandlerMapping, runs HandlerInterceptor.preHandle, resolves and validates…
-
Explain Java concept: try-with-resources.
try-with-resources automatically closes AutoCloseable resources at the end of the block, even when an exception is thrown.
-
Concurrency in Java: ExecutorService?
ExecutorService manages task execution with worker threads so application code submits work instead of manually creating threads for every task.
-
Concurrency in Java: deadlock?
Deadlock happens when threads wait forever for locks or resources held by each other in a cycle.
-
Concurrency in Java: race condition?
A race condition occurs when program correctness depends on unpredictable timing between threads accessing shared state.
-
Explain Java concept: bytecode.
Bytecode is the platform-neutral instruction format stored in .class files and executed by the JVM.
-
Explain Java concept: JIT compilation.
JIT compilation converts frequently executed bytecode into optimized native code at runtime.
-
Explain Java concept: final / finally / finalize.
final restricts reassignment or overriding, finally runs cleanup code after try/catch, and finalize was a deprecated object-finalization hook that should not be used.
-
Explain Java concept: static.
static members belong to the class rather than to an individual object instance.
-
Explain Java concept: composition over inheritance.
Composition over inheritance means building behavior by holding and delegating to collaborators instead of extending classes by default.
-
Explain Java concept: access modifiers.
Java access modifiers control where classes, fields, methods, and constructors are visible.
-
Explain Java concept: records.
A record is a compact Java type for immutable data carriers with generated constructor, accessors, equals, hashCode, and toString.
-
Java Collections: HashSet?
HashSet stores unique elements using a backing HashMap, so uniqueness depends on equals and hashCode.
-
Java Collections: TreeMap / TreeSet?
TreeMap and TreeSet keep entries sorted using natural ordering or a Comparator, with O(log n) core operations.
-
Java Collections: PriorityQueue?
PriorityQueue is a heap-based queue that removes the smallest element according to natural ordering or a Comparator.
-
Java Collections: Generics type erasure?
Type erasure means Java generic type parameters are checked at compile time but mostly removed from bytecode at runtime.
-
Concurrency in Java: Thread lifecycle?
A Java thread moves through states such as NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED.
-
Concurrency in Java: volatile?
volatile guarantees visibility and ordering for a variable, but it does not make compound actions atomic.
-
Concurrency in Java: happens-before?
happens-before is the Java Memory Model relationship that guarantees one action's effects are visible to another action.
-
Concurrency in Java: Future / CompletableFuture?
Future represents a pending asynchronous result; CompletableFuture adds composition, callbacks, manual completion, and async pipelines.
-
Concurrency in Java: virtual threads (Project Loom)?
Virtual threads are lightweight JVM-managed threads that make blocking-style code scale to many concurrent tasks.
-
JVM memory: GC generations?
Generational garbage collection separates objects by age because most objects die young while some live much longer.
-
JVM memory: memory leaks in Java?
A Java memory leak happens when objects are no longer useful but remain reachable, so garbage collection cannot reclaim them.
-
JVM memory: OutOfMemoryError?
OutOfMemoryError means the JVM could not allocate required memory in a memory area such as heap, metaspace, direct buffer memory, or native thread stacks.
-
Explain Java concept: String pool.
The String pool stores interned string instances so equal literals can share one object.
-
Explain Java concept: sealed classes.
Sealed classes and interfaces restrict which types may extend or implement them.
-
Explain Java concept: var (local type inference).
var lets the compiler infer the type of a local variable from its initializer.
-
Java Collections: LinkedHashMap?
LinkedHashMap is a HashMap variant that maintains a predictable iteration order using a linked list across entries.
-
Concurrency in Java: ThreadLocal?
ThreadLocal stores a separate value per thread, usually for per-request context or expensive non-thread-safe helpers.
-
Concurrency in Java: CountDownLatch / Semaphore?
CountDownLatch waits until a fixed count reaches zero; Semaphore limits how many permits can be acquired concurrently.
-
JVM memory: mark and sweep?
Mark-and-sweep garbage collection marks reachable objects and then reclaims unmarked objects.
-
JVM memory: escape analysis?
Escape analysis is a JIT optimization that determines whether an object is visible outside a method or thread and can therefore be optimized.