Interview Q&A · 52 questions · 13 answered publicly
Java Developer Interview Questions
Ordered by how often they're asked. Sign in free to see every answer, rate your confidence and run timed mock interviews.
-
Most asked Explain Java concept: JVM.
Summary
The JVM is the runtime that loads Java bytecode, verifies it, manages memory, and executes code through interpretation and JIT compilation.
Longer Answer
A Java program compiles to platform-neutral bytecode, and the JVM turns that bytecode into work on a specific operating system and CPU. It provides class loading, bytecode verification, execution, garbage collection, thread scheduling integration, and runtime services such as reflection and security checks.
In production, the JVM matters because performance and reliability are often controlled by heap sizing, garbage collector choice, JIT warm-up, thread behavior, and observability through logs, metrics, thread dumps, heap dumps, and Java Flight Recorder.
Key Points
- A Java program compiles to platform-neutral bytecode, and the JVM turns that bytecode into work on a specific operating system and CPU
- It provides class loading, bytecode verification, execution, garbage collection, thread scheduling integration, and runtime services such as reflection and security checks
- In production, the JVM matters because performance and reliability are often controlled by heap sizing, garbage collector choice, JIT warm-up, thread behavior, and observability through logs, metrics, thread dumps, heap dumps, and Java Flight Recorder
- For 'Explain Java concept: JVM.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea The JVM is the runtime that loads Java bytecode, verifies it, manages memory, and executes code through interpretation and JIT compilation. Practical tradeoff For 'Explain Java concept: JVM.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Explain Java concept: == vs equals.
Summary
For primitives, == compares values; for objects, == compares references, while equals is intended to compare logical equality.
Longer Answer
Using == on object references answers whether two variables point to the exact same object. Using equals should answer whether two objects represent the same value according to the class contract. Classes such as String, BigDecimal, LocalDate, and most value objects override equals.
A correct equals implementation must be reflexive, symmetric, transitive, consistent, and return false for null. If equals is overridden, hashCode must also be overridden so hash-based collections work correctly.
Key Points
- Using == on object references answers whether two variables point to the exact same object
- Using equals should answer whether two objects represent the same value according to the class contract
- Classes such as String, BigDecimal, LocalDate, and most value objects override equals
- For 'Explain Java concept: == vs equals.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea For primitives, == compares values; for objects, == compares references, while equals is intended to compare logical equality. Practical tradeoff For 'Explain Java concept: == vs equals.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Explain Java concept: hashCode contract.
Summary
Objects that are equal according to equals must return the same hashCode, or hash-based collections become incorrect.
Longer Answer
HashMap and HashSet use hashCode to choose a bucket and then use equals to find the exact key or element. If two equal objects produce different hash codes, lookups can fail even though equals says the objects match.
The contract does not require unequal objects to have different hash codes, but better distribution reduces collisions and improves performance. Fields used in equals should normally be the fields used in hashCode, and mutable keys should be avoided.
Key Points
- HashMap and HashSet use hashCode to choose a bucket and then use equals to find the exact key or element
- If two equal objects produce different hash codes, lookups can fail even though equals says the objects match
- The contract does not require unequal objects to have different hash codes, but better distribution reduces collisions and improves performance
- For 'Explain Java concept: hashCode contract.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea Objects that are equal according to equals must return the same hashCode, or hash-based collections become incorrect. Practical tradeoff For 'Explain Java concept: hashCode contract.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Explain Java concept: String immutability.
Summary
Java String is immutable: once created, its character content cannot be changed.
Longer Answer
String immutability makes strings safe to share, cache, intern, and use as map keys. Operations such as concat, replace, substring, and toLowerCase create new String objects rather than modifying the original.
The tradeoff is allocation. Repeated concatenation in loops can create many temporary objects, so StringBuilder or StringBuffer is preferred for incremental construction. Since modern Java, compact strings may store Latin-1 data more efficiently, but immutability remains the semantic contract.
Key Points
- String immutability makes strings safe to share, cache, intern, and use as map keys
- Operations such as concat, replace, substring, and toLowerCase create new String objects rather than modifying the original
- The tradeoff is allocation
- For 'Explain Java concept: String immutability.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea Java String is immutable: once created, its character content cannot be changed. Practical tradeoff The tradeoff is allocation. -
Most asked Explain Java concept: checked vs unchecked exceptions.
Summary
Checked exceptions must be declared or handled at compile time; unchecked exceptions are RuntimeException or Error subclasses and do not require declaration.
Longer Answer
Checked exceptions are useful when the caller can reasonably recover or must make an explicit decision, such as retrying, asking for a different file, or reporting an external system failure. Unchecked exceptions usually represent programming errors, invalid state, bad arguments, or failures that cannot be meaningfully recovered at every call site.
In modern service code, teams often translate low-level checked exceptions into domain-specific unchecked exceptions at boundaries. The important interview answer is not that one is always better; it is that exception type should match recoverability and API clarity.
Key Points
- Checked exceptions are useful when the caller can reasonably recover or must make an explicit decision, such as retrying, asking for a different file, or reporting an external system failure
- Unchecked exceptions usually represent programming errors, invalid state, bad arguments, or failures that cannot be meaningfully recovered at every call site
- In modern service code, teams often translate low-level checked exceptions into domain-specific unchecked exceptions at boundaries
- For 'Explain Java concept: checked vs unchecked exceptions.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea Checked exceptions must be declared or handled at compile time; unchecked exceptions are RuntimeException or Error subclasses and do not require declaration. Practical tradeoff For 'Explain Java concept: checked vs unchecked exceptions.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Explain Java concept: overloading vs overriding.
Summary
Overloading chooses between methods with the same name but different parameters at compile time; overriding replaces inherited behavior at runtime through polymorphism.
Longer Answer
Overloaded methods live in the same class or inheritance hierarchy and differ by parameter list. Return type alone is not enough to overload a method. The compiler selects the overload based on the declared argument types.
Overriding happens when a subclass provides a compatible implementation of an inherited instance method. The selected method is resolved dynamically based on the actual object type, which is the foundation of runtime polymorphism.
Key Points
- Overloaded methods live in the same class or inheritance hierarchy and differ by parameter list
- Return type alone is not enough to overload a method
- The compiler selects the overload based on the declared argument types
- For 'Explain Java concept: overloading vs overriding.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea Overloading chooses between methods with the same name but different parameters at compile time; overriding replaces inherited behavior at runtime through polymorphism. Practical tradeoff For 'Explain Java concept: overloading vs overriding.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Explain Java concept: abstract class vs interface.
Summary
Use an abstract class when related types share state or base behavior; use an interface to define a capability or contract that many unrelated classes can implement.
Longer Answer
An abstract class can have fields, constructors, concrete methods, and abstract methods. It is single-inheritance, so choosing it creates a strong type-family relationship.
An interface defines a contract. Modern Java interfaces can include default and static methods, but they still cannot hold per-instance state. A class can implement many interfaces, which makes interfaces better for capabilities such as Comparable, Runnable, or Repository.
Key Points
- An abstract class can have fields, constructors, concrete methods, and abstract methods
- It is single-inheritance, so choosing it creates a strong type-family relationship
- An interface defines a contract
- For 'Explain Java concept: abstract class vs interface.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea Use an abstract class when related types share state or base behavior; use an interface to define a capability or contract that many unrelated classes can implement. Practical tradeoff For 'Explain Java concept: abstract class vs interface.', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Java Collections: ArrayList vs LinkedList?
Summary
ArrayList is usually the default list because indexed access is fast and cache-friendly; LinkedList only helps in narrow cases with frequent iterator-based insertions/removals.
Longer Answer
ArrayList stores elements in a resizable array. get by index is O(1), appending is amortized O(1), and iteration is fast because memory is contiguous. Inserting or removing in the middle requires shifting elements.
LinkedList stores nodes with next/previous links. It can insert or remove quickly when you already have the node position through an iterator, but finding that position is O(n). Node allocation and poor cache locality often make it slower than people expect.
Key Points
- ArrayList stores elements in a resizable array
- get by index is O(1), appending is amortized O(1), and iteration is fast because memory is contiguous
- Inserting or removing in the middle requires shifting elements
- For 'Java Collections: ArrayList vs LinkedList?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea ArrayList is usually the default list because indexed access is fast and cache-friendly; LinkedList only helps in narrow cases with frequent iterator-based insertions/removals. Practical tradeoff For 'Java Collections: ArrayList vs LinkedList?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Java Collections: HashMap?
Summary
HashMap stores key-value pairs using hashCode to choose a bucket and equals to find the matching key.
Longer Answer
Average get and put are O(1) when keys have stable hash codes and a good distribution. Since Java 8, heavily-collided buckets can treeify into balanced trees after thresholds are met, improving worst-case lookup behavior.
HashMap is not synchronized and does not preserve insertion order. Keys must be effectively immutable with respect to equals/hashCode while stored in the map, or lookups and removals can fail.
Key Points
- Average get and put are O(1) when keys have stable hash codes and a good distribution
- Since Java 8, heavily-collided buckets can treeify into balanced trees after thresholds are met, improving worst-case lookup behavior
- HashMap is not synchronized and does not preserve insertion order
- For 'Java Collections: HashMap?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea HashMap stores key-value pairs using hashCode to choose a bucket and equals to find the matching key. Practical tradeoff For 'Java Collections: HashMap?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Java Collections: ConcurrentHashMap?
Summary
ConcurrentHashMap is a thread-safe hash map optimized for concurrent access without synchronizing the entire map for every operation.
Longer Answer
Modern ConcurrentHashMap uses fine-grained synchronization and CAS-style operations internally so many reads and updates can proceed concurrently. Reads are usually non-blocking, and update operations lock only the affected bin when needed.
It does not allow null keys or values because null would make concurrent get results ambiguous. For compound actions, prefer atomic methods such as computeIfAbsent, compute, merge, and putIfAbsent instead of separate check-then-act logic.
Key Points
- Modern ConcurrentHashMap uses fine-grained synchronization and CAS-style operations internally so many reads and updates can proceed concurrently
- Reads are usually non-blocking, and update operations lock only the affected bin when needed
- It does not allow null keys or values because null would make concurrent get results ambiguous
- For 'Java Collections: ConcurrentHashMap?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea ConcurrentHashMap is a thread-safe hash map optimized for concurrent access without synchronizing the entire map for every operation. Practical tradeoff For compound actions, prefer atomic methods such as computeIfAbsent, compute, merge, and putIfAbsent instead of separate check-then-act logic. -
Most asked Concurrency in Java: synchronized?
Summary
synchronized provides mutual exclusion and memory visibility using an object's monitor lock.
Longer Answer
A synchronized instance method locks this; a synchronized static method locks the Class object; a synchronized block locks the explicit object. Only one thread can hold a given monitor at a time, so protected critical sections execute mutually exclusively.
synchronized also establishes happens-before relationships: exiting a synchronized block flushes changes, and entering a block guarded by the same monitor makes those changes visible. The tradeoff is contention, potential deadlock, and less flexible lock control than java.util.concurrent locks.
Key Points
- A synchronized instance method locks this; a synchronized static method locks the Class object; a synchronized block locks the explicit object
- Only one thread can hold a given monitor at a time, so protected critical sections execute mutually exclusively
- synchronized also establishes happens-before relationships: exiting a synchronized block flushes changes, and entering a block guarded by the same monitor makes those changes visible
- For 'Concurrency in Java: synchronized?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea synchronized provides mutual exclusion and memory visibility using an object's monitor lock. Practical tradeoff The tradeoff is contention, potential deadlock, and less flexible lock control than java.util.concurrent locks. -
Most asked JVM memory: heap vs stack?
Summary
The heap stores objects shared across method calls, while each thread's stack stores call frames, local variables, and return information.
Longer Answer
Objects are generally allocated on the heap and reclaimed by garbage collection when no longer reachable. References to those objects can live in stack frames, object fields, static fields, registers, or optimized runtime structures.
Each thread has its own stack. Recursive calls or very deep call chains can cause StackOverflowError, while too many live objects or too small a heap can cause OutOfMemoryError. The JVM may optimize allocation through escape analysis, so the conceptual model and physical implementation can differ.
Key Points
- Objects are generally allocated on the heap and reclaimed by garbage collection when no longer reachable
- References to those objects can live in stack frames, object fields, static fields, registers, or optimized runtime structures
- Each thread has its own stack
- For 'JVM memory: heap vs stack?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code.
A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service
Comparison / Tradeoffs
Aspect Notes Core idea The heap stores objects shared across method calls, while each thread's stack stores call frames, local variables, and return information. Practical tradeoff For 'JVM memory: heap vs stack?', the important signal is that you can explain the concept and also connect it to correctness, performance, and maintainability in production Java code. A strong answer names the mechanism, describes where mistakes happen, and states the practical rule you would follow when designing or debugging a service. |
-
Most asked Describe the request flow in a Spring MVC (Spring Boot) application.
Summary
The servlet container runs the filter chain (including Spring Security), then the DispatcherServlet finds a handler via HandlerMapping, runs HandlerInterceptor.preHandle, resolves and validates arguments (message converters, @Valid), invokes the controller, writes the return value (or renders a view after postHandle), applies exception resolvers such as @ControllerAdvice when something throws, runs afterCompletion, and the response travels back out through the filters.
Longer Answer
@Transactional and @Cacheable apply through proxies around service calls inside the controller step. Knowing the order explains why exceptions thrown in filters bypass @ControllerAdvice and why interceptors can see which handler will run but filters can't.
Key Points
- @Transactional and @Cacheable apply through proxies around service calls inside the controller step
- Knowing the order explains why exceptions thrown in filters bypass @ControllerAdvice and why interceptors can see which handler will run but filters can't
-
What interviewers look for
A correct ordered flow naming filters, interceptors and exception resolvers
- Use the answer to connect the concept back to a realistic production or interview scenario for 'Describe the request flow in a Spring MVC (Spring Boot) application.'
Comparison / Tradeoffs
Aspect Notes Core idea The servlet container runs the filter chain (including Spring Security), then the DispatcherServlet finds a handler via HandlerMapping, runs HandlerInterceptor.preHandle, resolves and validates arguments (message converters, @Valid), invokes the controller, writes the return value (or renders a view after postHandle), applies exception resolvers such as @ControllerAdvice when something throws, runs afterCompletion, and the response travels back out through the filters. Practical tradeoff ## What interviewers look for A correct ordered flow naming filters, interceptors and exception resolvers. |
- Explain Java concept: JRE vs JDK.
- Explain Java concept: primitive vs reference types.
- Explain Java concept: try-with-resources.
- Concurrency in Java: ExecutorService?
- Concurrency in Java: deadlock?
- Concurrency in Java: race condition?
- Explain Java concept: bytecode.
- Explain Java concept: JIT compilation.
- Explain Java concept: final / finally / finalize.
- Explain Java concept: static.
- Explain Java concept: composition over inheritance.
- Explain Java concept: access modifiers.
- Explain Java concept: records.
- Java Collections: HashSet?
- Java Collections: TreeMap / TreeSet?
- Java Collections: PriorityQueue?
- Java Collections: Comparable vs Comparator?
- Java Collections: Generics type erasure?
- Concurrency in Java: Thread lifecycle?
- Concurrency in Java: volatile?
- Concurrency in Java: happens-before?
- Concurrency in Java: Callable vs Runnable?
- Concurrency in Java: Future / CompletableFuture?
- Concurrency in Java: virtual threads (Project Loom)?
- JVM memory: GC generations?
- JVM memory: memory leaks in Java?
- JVM memory: OutOfMemoryError?
- What is the difference between a servlet Filter and a HandlerInterceptor, and why doesn't @ControllerAdvice handle exceptions thrown in filters?
- Explain Java concept: String pool.
- Explain Java concept: sealed classes.
- Explain Java concept: var (local type inference).
- Java Collections: LinkedHashMap?
- Java Collections: fail-fast iterators?
- Concurrency in Java: ThreadLocal?
- Concurrency in Java: CountDownLatch / Semaphore?
- JVM memory: mark and sweep?
- JVM memory: escape analysis?
- What is the Java Virtual Machine (JVM) and why is it important?
- What is the difference between checked and unchecked exceptions in Java?