Java Interview Questions: HashMap, ConcurrentHashMap, Thread Safety, and More

Top 10 Most Asked Java Interview Questions 1. What is the difference between HashMap and ConcurrentHashMap? HashMap is not thread-safe and can cause data inconsistency in concurrent environments. ConcurrentHashMap uses fine-grained locking and CAS operations to allow safe concurrent reads and writes. 2. What is the difference between final, finally, and finalize? final → keyword used for variables, methods, and classes to restrict modification. finally → block used in exception handling to execute cleanup code. finalize → method used by GC before object collection (deprecated and rarely used). 3. What happens when hashCode() and equals() are not implemented properly? If equal objects produce different hashCodes, collections like HashMap behave incorrectly. This leads to duplicate keys and degraded lookup performance. 4. What is the difference between ArrayList and LinkedList? ArrayList uses a dynamic array and provides fast random access. LinkedList uses a doubly linked structure and is efficient for insertions and deletions. 5. What is the difference between Thread and Runnable? Thread is a class that represents a thread of execution. Runnable is an interface used to define the task that a thread executes. Using Runnable improves flexibility because it separates the task from thread management. 6. What causes memory leaks in Java? Common causes include: - Static collections holding references - Unclosed resources - ThreadLocal misuse - Listeners not removed Heap dumps and tools like MAT help identify leaks. 7. What is the difference between synchronized and Lock? synchronized is simpler and managed by JVM. Lock provides advanced features like tryLock(), fairness policies, and interruptible locks. 8. What is the difference between checked and unchecked exceptions? Checked exceptions must be handled at compile time. Unchecked exceptions occur at runtime and usually indicate programming errors. 9. What is the difference between fail-fast and fail-safe iterators? Fail-fast iterators throw ConcurrentModificationException if the collection is modified during iteration. Fail-safe iterators operate on a copy of the collection. 10. What is the JVM and what are its main components? JVM is the runtime that executes Java bytecode. Main components include: - Heap - Stack - Method Area - Garbage Collector - JIT Compiler #Java #JavaInterview #BackendEngineering #SpringBoot #CodingInterview #SoftwareEngineering

To view or add a comment, sign in

Explore content categories