Core Java interview questions on OOP, collections, multithreading, JVM internals, and design patterns.
8 Questions Detailed Answers
1What is the difference between abstract class and interface?
Easy
View Answer
Abstract class: can have constructors, instance variables, concrete methods, single inheritance. Interface: no constructors (until Java 8 default methods), multiple inheritance, all methods implicitly public. Use abstract class for "is-a" with shared code, interface for "can-do" capabilities.
2Explain HashMap internal working.
Hard
View Answer
HashMap uses an array of buckets. `put(key, value)`: (1) Compute hash of key, (2) Find bucket index = hash & (n-1), (3) If collision, use linked list (or red-black tree if ≥8 nodes in Java 8+). `get(key)`: same hash → same bucket → traverse list comparing keys with equals(). Load factor 0.75 triggers rehashing.
3What is the difference between String, StringBuilder, and StringBuffer?
Easy
View Answer
String: immutable (creates new object on modification). StringBuilder: mutable, not thread-safe, faster. StringBuffer: mutable, thread-safe (synchronized), slower. Use StringBuilder in single-threaded code for string manipulation, StringBuffer in multi-threaded scenarios.
4Explain Java memory model (Stack vs Heap).
Medium
View Answer
Stack: stores method frames, local variables, references. Thread-specific, LIFO. Heap: stores objects and instance variables. Shared across threads. Stack is faster but smaller. `int x = 5` → stack. `new Object()` → heap (reference on stack). Garbage collector manages heap memory.
5What are Java streams? Give an example.
Medium
View Answer
Streams provide functional-style operations on collections. They're lazy and can be parallelized. Example: `employees.stream().filter(e -> e.getSalary() > 50000).map(Employee::getName).sorted().collect(Collectors.toList())`. Key operations: filter, map, reduce, collect, flatMap.
6Explain the SOLID principles.
Medium
View Answer
S: Single Responsibility — one class, one reason to change. O: Open/Closed — open for extension, closed for modification. L: Liskov Substitution — subclass should be substitutable for parent. I: Interface Segregation — many specific interfaces > one general. D: Dependency Inversion — depend on abstractions, not concretions.
7What is the difference between checked and unchecked exceptions?
Easy
View Answer
Checked exceptions (IOException, SQLException): must be caught or declared with throws. Detected at compile time. Unchecked exceptions (NullPointerException, ArrayIndexOutOfBoundsException): extend RuntimeException, no requirement to handle. Errors (OutOfMemoryError) should not be caught.
8What is a deadlock? How do you prevent it?
Hard
View Answer
Deadlock: two+ threads waiting for each other's locks indefinitely. Prevention: (1) Lock ordering — always acquire locks in the same order, (2) Lock timeout — tryLock with timeout, (3) Avoid nested locks, (4) Use concurrent utilities (ConcurrentHashMap, atomic variables).