Java interview questions
Java Interview Questions for Software Engineers
Prepare for Java interviews by connecting language fundamentals to production behavior. Strong answers explain not only what an API does, but also memory, concurrency, correctness, and trade-offs.
Core Java questions
Be ready to explain Java semantics precisely and connect them to examples from real code.
Is Java pass-by-value or pass-by-reference?
Always pass-by-value. For objects, the value that gets copied is the reference, so a method can change the object it points to but cannot make the caller's variable point to a different object.
What is the difference between equals() and ==, and why must hashCode() match equals()?
For objects, == compares references, while equals() compares logical equality as the class defines it. Objects that are equal must return the same hashCode(); otherwise HashMap and HashSet look in the wrong bucket and fail to find them.
When would you use an interface instead of an abstract class?
Use an interface for a capability that unrelated classes can share, since a class can implement many. Use an abstract class when subclasses share state or constructor logic. Default methods narrowed the gap in Java 8, but only abstract classes can hold instance fields.
When should an exception be checked, and when unchecked?
Checked exceptions suit recoverable conditions the caller is expected to handle, such as a missing file. Unchecked exceptions signal programming errors or failures the caller can't reasonably fix. Many modern codebases lean on unchecked exceptions to keep APIs and lambdas simple.
Collections and concurrency questions
Backend interviews spend a lot of time on how collections behave and what breaks under concurrent access.
How does HashMap work, and what happens on a collision?
The key's hashCode() is spread and masked to pick a bucket. Colliding keys share the bucket as a linked list, which Java 8+ turns into a balanced tree once a bucket holds more than 8 entries and the table has at least 64 buckets. The table doubles when its size passes capacity × load factor (0.75 by default).
ConcurrentHashMap or Collections.synchronizedMap?
synchronizedMap guards every call with one lock, so threads queue up. ConcurrentHashMap allows concurrent reads and fine-grained locking on writes, and offers atomic compound operations such as computeIfAbsent and merge. Prefer it for shared maps under contention.
synchronized or a Lock?
synchronized is simpler and always releases the lock. ReentrantLock adds tryLock with timeouts, interruptible waits, fairness, and multiple conditions, at the cost of having to unlock in a finally block yourself.
Future or CompletableFuture?
A Future only lets you block on get(). CompletableFuture lets you chain stages with thenApply and thenCompose, combine results, and handle errors without blocking. Give it your own executor for blocking I/O instead of the shared common pool.
JVM and production follow-ups
For experienced roles, prepare to discuss heap versus stack, garbage collection, memory leaks, thread pools, blocking calls, immutability, and how you would diagnose a Java service under load.
What is the difference between the JDK, JRE, and JVM?
The JVM runs bytecode. The JRE is the JVM plus the standard libraries needed to run programs. The JDK adds the compiler and developer tools. Since Java 11 the JDK is the normal download and a separate JRE is rarely shipped.
What lives on the heap and what lives on the stack?
Each thread's stack holds method frames with local primitives and references. Objects live on the shared heap and stay there until no live reference reaches them, after which the garbage collector can reclaim them.
What causes long GC pauses, and how would you reduce them?
Usually a high allocation rate, a heap that is too small, or large old-generation collections. Read the GC logs first, then cut allocation in hot paths, size the heap properly, tune G1, or move to a low-pause collector such as ZGC.
A Java service is at 100% CPU. How do you find out why?
Find the busiest threads (for example with top -H), take a few thread dumps with jstack or jcmd, and match the hot thread IDs to their stack traces. A profiler such as async-profiler or Java Flight Recorder then shows which methods use the CPU. Check GC logs to rule out GC thrashing.
Common questions
Should I memorize HashMap internals?
Understand the behavior and why it matters: hashing, bucket selection, collisions, equality, resizing, complexity, and thread-safety limitations. Memorizing implementation trivia without reasoning is less useful.
How do I practice Java interview follow-ups?
After answering a concept question, add a scenario: concurrency, performance, failure, testing, or API design. EvalcueAI's Java practice track asks those follow-ups for you.