Java interview questions and answers for collections, concurrency and design
Java interview questions become easier when you start from contracts rather than memorised class names. If a value can be used as a map key, what equality contract must hold? If work is concurrent, who owns shared state? If an API returns an optional value, what behaviour is explicit to the caller? The language and API references are available through Dev.java and the Java API documentation.
How Java questions show up in interviews
Interviewers often turn a basic question into a design follow-up: choose a collection for a workload, make a class safe to use as a key, or explain how a service shuts down its executors. The same habit of naming contracts and trade-offs helps with Node.js questions, TypeScript questions, and technical interview preparation.
Junior Java interview questions
1. What is the difference between == and equals?
For object references, == tests whether two references point to the same object. equals can define logical value equality. Immutable value objects often override equals so two separate instances with the same fields compare equal.
2. Why must hashCode agree with equals?
Hash-based collections use a hash to find a candidate bucket and equals to confirm a match. If equal objects return different hash codes, a HashMap or HashSet can fail to find a value that was inserted. Override both together and keep fields used by them stable while the object is a key.
3. What is the difference between ArrayList and LinkedList?
ArrayList stores elements in a resizable array and gives fast indexed access. LinkedList links nodes and can make certain insertions cheap once you already have the node, but traversal and allocation costs often make ArrayList the better default. Choose from measured access patterns.
4. What is an interface for?
An interface defines a contract that multiple implementations can satisfy. It lets callers depend on behaviour rather than a concrete class, which makes substitution and testing easier. Do not create an interface for every class without a real boundary or alternate implementation.
5. What is the difference between checked and unchecked exceptions?
Checked exceptions must be declared or handled by callers; unchecked exceptions extend RuntimeException and do not. Use exceptions to communicate abnormal failure conditions, not ordinary control flow. The important design question is whether a caller can reasonably recover.
Mid-level Java interview questions
6. What do generics provide?
Generics let APIs express the type of values they accept and return, catching many mistakes at compile time. Because of type erasure, do not assume every generic type is available at runtime. Learn the difference between ? extends T for producers and ? super T for consumers.
7. When should you use Optional?
Optional makes the possible absence of a return value explicit. It is usually useful at API boundaries, especially for a result that may not exist. Avoid using it as a field or parameter by default; callers can be forced into awkward nesting without gaining clarity.
8. What is the difference between a Stream and a collection?
A collection stores values; a Stream describes a pipeline that processes values. A Stream is normally consumed once and can be lazy. Use it where the pipeline is clearer, but do not turn a simple loop into a dense chain that hides branching or error handling.
9. Why is String immutable?
Immutability makes strings safe to share, suitable for hashing and simpler to reason about in concurrent code. The practical interview point is not merely that String cannot change; it is that operations create a new value, so repeated concatenation in a loop may deserve a StringBuilder.
10. What is dependency injection?
Dependency injection supplies a component's collaborators rather than constructing every collaborator inside it. Constructor injection makes required dependencies visible and makes tests easier to set up. A framework can automate wiring, but the design principle exists without one.
Senior Java interview questions
11. What does synchronized protect?
It coordinates access to a monitor and establishes visibility guarantees around that synchronization. It is not a magic performance fix. Define the shared invariant first, then choose a lock, an immutable design, an atomic type or a higher-level concurrent collection.
12. What is volatile for?
Volatile gives visibility and ordering guarantees for reads and writes of a variable; it does not make a compound operation such as increment atomic. It is useful for a flag or published reference when that limited contract is enough.
13. How would you use an ExecutorService safely?
Choose a bounded, observable executor appropriate to the workload, handle task failures, set timeouts where relevant, and shut it down during application lifecycle termination. Avoid creating an unbounded pool simply because it removes immediate queue pressure.
14. What makes an object immutable?
Its observable state cannot change after construction: fields are private and final where possible, mutable inputs are defensively copied, and no method exposes mutable internals. Immutability reduces shared-state bugs and makes value semantics easier to trust.
15. How do you investigate a slow Java service?
Start from a symptom and measure: request latency, allocation rate, database waits, thread-pool queues and garbage-collection behaviour. Capture a profile or thread dump safely, form a hypothesis, then change one thing. Avoid declaring the JVM or GC guilty without evidence.
How to give a stronger Java answer
State the contract, then the implementation choice, then its cost. For example: HashMap needs stable equality and hash-code behaviour; I would use an immutable key and verify the lookup with a test. That explanation is more persuasive than listing classes from memory.
Ask the recruiter which Java version, libraries and tools are permitted in the assessment. The core contracts outlast version-specific syntax.
Sources
- Dev.java — official documentation
FAQ
What Java interview questions are most common?
Expect equality and hashCode, collections, generics, exceptions, Optional, streams, dependency injection, concurrency, immutability, JVM performance and testing.
How do I explain equals and hashCode?
equals defines logical equality and hashCode helps hash-based collections find candidate values. Equal objects must return the same hash code, and fields used by both should not change while an object is a map key.
When should I use ArrayList versus LinkedList?
ArrayList is usually the practical default for indexed access and traversal. LinkedList can help in narrow cases where you already have a node and repeatedly insert or remove around it; measure the actual workload.
What is the difference between volatile and synchronized?
volatile gives visibility and ordering for a variable but not atomicity for compound operations. synchronized coordinates access through a monitor and can protect a larger shared invariant.
How should I prepare for a Java live coding interview?
Practise choosing collections from constraints, writing small tests, explaining equality and complexity, and narrating ownership of shared state. Confirm the permitted Java version and libraries with the recruiter.