Java Interview Questions
Explain what a Java API guarantees before relying on its implementation or performance. The scenarios cover both code behavior and service diagnosis.
Try answering aloud before opening each explanation.
Try the Java practice demoFoundations
What contract must equals and hashCode satisfy?
Two equal objects must have equal hash codes; unequal objects may collide. Do not mutate equality-relevant fields while an object is used as a hash-map key.
When would ArrayList be preferable to LinkedList?
ArrayList supports fast indexed access and good locality. LinkedList only makes insertion cheap once you already have the position; finding it can still require a scan.
Why is List<Integer> not assignable to List<Number>?
If it were, the recipient could insert a Double into a list meant only for integers. Use List<? extends Number> when reading numeric values without adding new ones.
What is the practical difference between checked and unchecked exceptions?
A checked exception must be caught or declared by callers at compile time. Unchecked exceptions have no such requirement, but still need handling at a boundary where recovery or reporting is meaningful.
Does final make a Java object immutable?
A final reference cannot be assigned again, but the object may still mutate. Immutable types must also prevent mutation through fields and references they expose.
Applied
Why does volatile not make count++ thread-safe?
It establishes visibility and ordering for accesses, not atomicity of the read-modify-write sequence. Use an atomic operation or a lock that covers the whole update.
Is get followed by put an atomic operation on ConcurrentHashMap?
No. Another thread can act between the calls. Use operations such as putIfAbsent or compute where their atomic single-key semantics match the requirement.
What does try-with-resources guarantee when closing also throws?
It closes initialized resources in reverse order. If the body already threw, close failures are attached as suppressed exceptions rather than replacing the primary failure.
Why should shared mutable state be avoided in a parallel stream?
Pipeline operations may run concurrently and in a different order than expected. Use a collector or reduction with appropriate semantics rather than mutating an external list.
What does thenCompose do differently from thenApply?
ThenApply maps a successful result to a new value. When the mapping starts another completion stage, thenCompose flattens it so callers wait on a single stage for the final value.
Production scenarios
How do you diagnose a Java process whose heap never returns to its old baseline?
Compare live memory after equivalent workloads and collections. Inspect retained objects and their paths to roots before increasing heap size; unbounded caches and live listeners are common retention sources.
Why can an unbounded executor queue cause high latency without high CPU?
Tasks may wait behind blocked workers while the queue grows. Measure queue age and dependency wait, then bound the queue and choose an explicit rejection or backpressure policy.
Do virtual threads remove the need to limit database concurrency?
No. They reduce the cost of many blocked Java tasks, not the database's capacity or available connections. Bound scarce resources explicitly and measure waits.
How do you distinguish lock contention from a CPU bottleneck?
Sample thread states and use profiling or Flight Recorder. Many blocked threads with a hot monitor point to contention; high CPU with runnable stacks points toward computation or spin.
What should a service do when interrupted while blocking?
Propagate InterruptedException when the contract allows it. If translating it, typically restore the interrupt flag and exit or cancel work rather than silently proceeding.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Docker interview questions
- MongoDB interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions