Java Advanced
Java Interview Questions and Answers
Concise answers to questions that come up repeatedly. Practise explaining each in your own words.
Core language
| Question | Answer |
|---|---|
== 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
| Question | Answer |
|---|---|
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
| Question | Answer |
|---|---|
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
| Question | Answer |
|---|---|
| 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
| Feature | Version | Use |
|---|---|---|
var | 10 | Local type inference |
| Switch expressions | 14 | Concise, exhaustive switches |
| Text blocks | 15 | Multi-line strings |
| Records | 16 | Immutable data classes |
| Sealed classes, pattern matching | 17+ | Closed hierarchies, safer branching |
| Virtual threads | 21 | Lightweight 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.
Finished reading?
Mark it complete to track your progress across the docs.
Questions and discussion
Stuck on something here, or found a clearer way to explain it? Start a thread.
- Ask a question or share what helped
- React to useful answers
- Sign in with GitHub to post
Comments are stored in GitHub Discussions and shown here by giscus. PrepVerse never sees your GitHub password.