Skip to main content

Java Interview Questions and Answers

Concise answers to questions that come up repeatedly. Practise explaining each in your own words.

Core language​

QuestionAnswer
== vs equals()?== compares references (or primitive values); equals compares logical content. Override equals and hashCode together.
Why is String immutable?Safety (keys, class loading, security), thread-safety, string pool caching, cached hash code.
final, finally, finalize?final: constant / cannot override or extend. finally: always-run block. finalize: deprecated cleanup hook; do not use.
Abstract class vs interface?Interface = contract (multiple allowed); abstract class = shared state and partial implementation (single).
Checked vs unchecked exceptions?Checked must be handled or declared (IOException); unchecked extend RuntimeException.
What is autoboxing?Automatic conversion between primitives and wrappers; beware NullPointerException and Integer caching (==).

Collections​

QuestionAnswer
How does HashMap work?Array of buckets indexed by hash(key); collisions chain in a list or tree (Java 8+ treeifies at 8 entries); resize at load factor 0.75. Average O(1).
HashMap vs ConcurrentHashMap?ConcurrentHashMap is thread-safe with fine-grained locking/CAS and no null keys or values.
ArrayList vs LinkedList?ArrayList: O(1) index, O(n) middle insert. LinkedList: O(1) at known node but O(n) access; ArrayList is almost always faster.
What is the contract between equals and hashCode?Equal objects must have equal hash codes; not vice versa.
Comparable vs Comparator?Natural ordering inside the class vs external, multiple orderings.
Fail-fast iterators?Throw ConcurrentModificationException if the collection is structurally modified during iteration.

Concurrency​

QuestionAnswer
synchronized vs ReentrantLock?synchronized is simple and automatic; ReentrantLock adds try-lock, timeouts, fairness and multiple conditions.
What does volatile guarantee?Visibility and ordering of writes between threads; not atomicity of compound operations.
Runnable vs Callable?Callable returns a result and can throw checked exceptions.
What is a deadlock and how do you avoid it?Circular lock waits; acquire locks in a consistent order, use timeouts, minimise lock scope.
Why use thread pools?Reuse threads, bound concurrency and resource use.

JVM​

QuestionAnswer
Stack vs heap?Stack: per-thread frames and locals. Heap: shared objects, garbage collected.
How does garbage collection decide what to free?Reachability from GC roots; unreachable objects are collected.
What causes OutOfMemoryError?Heap exhaustion from a leak or too small a heap; also metaspace or native memory.
JDK vs JRE vs JVM?JVM runs bytecode; JRE = JVM + libraries; JDK = JRE + tools.

Modern Java​

FeatureVersionUse
var10Local type inference
Switch expressions14Concise, exhaustive switches
Text blocks15Multi-line strings
Records16Immutable data classes
Sealed classes, pattern matching17+Closed hierarchies, safer branching
Virtual threads21Lightweight concurrency

Coding practice​

Frequently asked in Java rounds: reverse a string, detect duplicates with a HashSet, implement an LRU cache with LinkedHashMap, producer-consumer with BlockingQueue, and thread-safe singleton. See Collections and generics and Concurrency for the underlying concepts, and Interview Prep for strategy.