Core Java Interview Questions (3 Years Experience)
1. What are the main principles of OOP in Java?
Answer: Java follows 4 main OOP principles:
1. Encapsulation: Hiding internal object state; providing access via getters/setters.
Example:
2. public class User {
3. private String name;
4. public String getName() { return name; }
5. public void setName(String name) { [Link] = name; }
6. }
7. Abstraction: Showing only necessary details via abstract classes or interfaces.
8. Inheritance: Reusing code from a parent class.
9. Polymorphism: Same method behaving differently (method overloading or
overriding).
Usage: Used in designing DTOs, service layers, and implementing multiple payment
strategies.
2. Difference between == and .equals() in Java
Answer:
• == → Compares reference (memory address).
• .equals() → Compares actual object content (must override in custom objects).
Example:
String a = new String("Java");
String b = new String("Java");
[Link](a == b); // false
[Link]([Link](b)); // true
3. Difference between ArrayList and LinkedList
Feature ArrayList LinkedList
Data Structure Dynamic array Doubly linked list
Access O(1) O(n)
Insert/Delete Slow (shift elements) Fast (just change pointers)
Memory Less overhead More memory (nodes + pointers)
Usage: For read-heavy lists → ArrayList, for frequent insert/delete → LinkedList.
4. How does HashMap work internally?
Answer:
• HashMap stores data in buckets (array of Node objects).
• Key’s hashCode determines index: hash(key) & (n-1).
• Collisions handled via LinkedList (≤ 8 elements) or Red-Black Tree (>8 elements).
• Default load factor = 0.75; resize triggers rehash.
Time Complexity: Average O(1), worst-case O(log n).
5. Difference between HashMap, TreeMap, LinkedHashMap
Feature HashMap TreeMap LinkedHashMap
Order No order Sorted order Insertion order
Null Key Allowed (1) Not allowed Allowed (1)
Performance O(1) avg O(log n) O(1) avg
6. What is String, StringBuilder, StringBuffer difference?
Feature String StringBuilder StringBuffer
Mutability Immutable Mutable Mutable
Thread Safety N/A Not synchronized Synchronized
Performance Slow (concatenation) Fast Slow (due to sync)
Example: Use StringBuilder in loops for efficient concatenation.
7. What are Java 8 features you used?
Answer:
• Lambda expressions: Concise implementation of functional interfaces.
• List<String> names = [Link]("Ram","John");
• [Link](name -> [Link](name));
• Stream API: Filter, map, reduce, collect.
• Optional: Avoid NullPointerException.
• Default & static methods in interfaces.
• Date/Time API: LocalDate, LocalDateTime.
8. What is multithreading? Difference between Runnable and Thread
Answer:
• Thread: Represents a thread of execution.
• Runnable: Functional interface; separates task from thread execution.
Example:
class MyTask implements Runnable {
public void run() {
[Link]("Running task");
Thread t = new Thread(new MyTask());
[Link]();
Usage: Thread pool executor for backend concurrent processing.
9. What is synchronized, volatile, Lock in Java?
• synchronized: Locks a method or block to allow single thread access.
• volatile: Ensures visibility across threads (prevents caching).
• Lock ([Link]): More advanced locking; allows tryLock().
Deadlock prevention: Always acquire locks in consistent order or use tryLock().
10. Difference between ==, equals(), and hashCode() contract
Answer:
• hashCode() + equals() must be consistent: if [Link](b) → [Link]() ==
[Link]().
• Used in HashMap/HashSet for proper storage and retrieval.
11. What is garbage collection? Types of references
Answer: Automatic memory management in Java.
Reference types:
• Strong: Normal object reference.
• Soft: GC may remove when memory low.
• Weak: GC removes in next cycle.
• Phantom: Used for cleanup, e.g., ReferenceQueue.
Usage: Minimize memory leaks in enterprise applications.
12. Difference between final, finally, and finalize
Keyword/Method Use
final Constant variable, prevent inheritance/method override
finally Block that executes always after try-catch
finalize() Called by GC before object destruction (deprecated in Java 9+)
13. Checked vs Unchecked Exceptions
• Checked: Must be declared or handled (IOException, SQLException).
• Unchecked: Runtime exceptions (NullPointerException,
ArrayIndexOutOfBoundsException).
Usage: Use checked for recoverable errors, unchecked for programming errors.
14. Java Memory Model: Stack vs Heap
• Stack: Stores primitive variables and references; thread-local; LIFO.
• Heap: Stores objects; shared across threads; managed by GC.
Example: DTOs are created on Heap, local variables on Stack.
15. Difference between abstract class and interface
Feature Abstract Class Interface
Methods Abstract + concrete Only abstract/default/static
Fields Can have variables public static final constants
Inheritance Single class Multiple interfaces possible
Usage: Use abstract class for base implementations, interface for contract definition.
16. What is Java Reflection? Usage
Answer: Allows inspection of classes, methods, fields at runtime.
Example: Dynamically load classes in Spring, frameworks.
Class<?> cls = [Link]("[Link]");
Method[] methods = [Link]();
17. What is Java Serialization and Deserialization?
Answer: Converts object → byte stream (serialization) and byte stream → object
(deserialization).
Usage: Caching, REST API data transfer, storing sessions.
Example: Implement Serializable interface.
18. What is transient keyword?
Answer: Marks a field not to be serialized.
Example:
private transient String password;
19. Difference between Comparator and Comparable
Feature Comparable Comparator
Implemented by Object itself Separate class
Method compareTo() compare()
Sorting Natural ordering Custom ordering
20. Difference between wait(), sleep(), notify(), notifyAll()
Method Purpose
wait() Releases lock, waits for notify/notifyAll
sleep() Thread sleeps, retains lock
notify() Wakes one waiting thread
notifyAll() Wakes all waiting threads