A working reference for Java developer interviews. Answers are kept short on purpose — enough to show you understand, not a lecture. If you want to practise these out loud with feedback, that is what interview preparation is for.
Core language
What is the difference between == and .equals()?
== compares references (are these the same object?). .equals() compares logical value, if the class overrides it. Always override hashCode() whenever you override equals() so the object behaves correctly in hash-based collections.
Explain final, finally and finalize.
final marks a variable, method or class as non-reassignable, non-overridable or non-extendable. finally is a block that always runs after try/catch, used for cleanup. finalize() was a method the GC could call before collecting an object; it is deprecated and should not be used — use try-with-resources or Cleaner instead.
What is the difference between an abstract class and an interface?
An abstract class can hold state and constructors and models an "is-a" relationship with shared implementation. An interface is a contract; since Java 8 it can have default and static methods but no instance state. A class extends one abstract class but can implement many interfaces.
What are checked and unchecked exceptions?
Checked exceptions (subclasses of Exception but not RuntimeException) must be declared or handled — they represent recoverable conditions. Unchecked exceptions (RuntimeException and subclasses) usually indicate programming errors and are not forced on callers.
What is autoboxing and where does it bite you?
Automatic conversion between primitives and their wrapper types. It bites you in performance-sensitive loops (unnecessary object creation), in == comparisons of Integer objects outside the -128..127 cache, and with NullPointerException when unboxing a null wrapper.
Collections
How does a HashMap work internally?
Keys are hashed to a bucket index. Each bucket holds entries in a linked list, which converts to a balanced tree once it passes a threshold (8) to keep worst-case lookups at O(log n). On resize the table doubles and entries are rehashed. Good hashCode() and immutable keys are essential.
When would you use ArrayList vs LinkedList?
ArrayList almost always. It has better cache locality and O(1) random access; add/remove at the end is amortised O(1). LinkedList only wins when you do frequent inserts/removals at both ends via the Deque interface, and even then ArrayDeque is usually better.
What is the difference between HashMap, LinkedHashMap and TreeMap?
HashMap has no ordering. LinkedHashMap preserves insertion order (or access order, useful for LRU caches). TreeMap keeps keys sorted and offers range queries, at O(log n) cost.
What is a ConcurrentModificationException and how do you avoid it?
It is thrown when a collection is structurally modified while being iterated with a fail-fast iterator. Avoid it by using the iterator's own remove(), collecting changes and applying them after the loop, using removeIf, or iterating a concurrent collection.
Concurrency
What is the difference between a process and a thread?
A process has its own memory space; threads share the heap of their process and have their own stack. Threads are cheaper to create and communicate through shared memory, which is also why synchronisation is needed.
What does the volatile keyword guarantee?
Visibility and ordering: a write to a volatile field is visible to other threads immediately, and it prevents certain instruction reordering around it. It does not provide atomicity for compound actions like count++ — use AtomicInteger or a lock for that.
synchronized vs ReentrantLock?
synchronized is simpler and released automatically. ReentrantLock adds tryLock, interruptible locking, fairness options and multiple condition variables, at the cost of a mandatory try/finally unlock.
What are virtual threads (Project Loom)?
Lightweight threads managed by the JVM rather than the OS, so you can have millions of them. They make blocking code scale like async code without the callback style. They are ideal for I/O-bound server workloads; they do not speed up CPU-bound work.
What is a thread pool and why use one?
A managed set of reusable worker threads fed by a task queue. It caps concurrency, avoids the cost of creating threads per task, and gives you back-pressure. Use ThreadPoolExecutor (or Executors factory methods, carefully — the unbounded-queue defaults can hide problems).
JVM and performance
Walk through what happens during garbage collection.
The GC identifies objects still reachable from GC roots and reclaims the rest. Modern collectors (G1, ZGC, Shenandoah) split work into short concurrent phases to keep pause times low. The generational hypothesis — most objects die young — is why the heap is split into young and old regions.
How would you diagnose a memory leak in a running Java service?
Watch heap usage over time (if it trends up and never recovers after GC, suspect a leak). Take a heap dump, open it in a tool like Eclipse MAT, look at the dominator tree for large retained sets, and trace the reference chain back to the root holding the memory — often a static collection or an unbounded cache.
A REST endpoint is slow. How do you find out why?
Confirm where the time goes: application vs database vs downstream calls. Add timing/tracing, check slow query logs, look at connection-pool saturation and GC pauses, and profile with async-profiler or JFR under load. Fix the biggest contributor first, then re-measure.
Spring
What is dependency injection and why does Spring use it?
Objects declare what they need rather than constructing it, and a container supplies the dependencies. It decouples components, makes them testable (inject mocks), and centralises configuration. Prefer constructor injection so dependencies are explicit and the object is immutable.
What is the difference between @Component, @Service, @Repository and @Controller?
Functionally they are all beans. The specific ones carry intent and, in the case of @Repository, translate persistence exceptions; @Controller/@RestController mark web handlers. Use the specific stereotype that matches the role.
How does Spring Boot auto-configuration work?
On startup Spring Boot inspects the classpath and your beans and conditionally configures things (@ConditionalOnClass, @ConditionalOnMissingBean, etc.). If the H2 driver is present and you have no DataSource, it configures an in-memory one; if you define your own, it backs off. --debug prints the auto-configuration report.
What is the N+1 query problem in JPA and how do you fix it?
Loading a list of N entities and then triggering one extra query per entity for a lazy association — N+1 queries total. Fix it with a JOIN FETCH, an entity graph, @BatchSize, or a projection that selects exactly what you need.
Practising these
Knowing the answer and delivering it calmly under interview pressure are different skills. If you have a Java interview coming up, a couple of mock interview sessions with a senior engineer will show you where you actually stand. If you are already in a Java role and struggling with the real thing rather than the interview, see Java job support.