Core Java Interview Questions and Answers
(Detailed Notes) - Aditya Bamanikar
1. OOPs Concepts
Q1. What are the four pillars of OOPs?
Encapsulation, Abstraction, Inheritance, Polymorphism.
Q2. Difference between abstraction and encapsulation?
Abstraction hides implementation; encapsulation binds data and methods.
Q3. Overloading vs Overriding:
Overloading → compile-time; Overriding → runtime.
Example:
class A { void show(int a) {} }
class B extends A { @Override void show(int a) {} }
2. String Handling
Q4. Why are Strings immutable?
Because they are stored in String Constant Pool for caching and security.
Q5. Difference between String, StringBuilder, StringBuffer:
String – immutable; StringBuilder – mutable, non-thread-safe; StringBuffer – thread-safe.
Example:
StringBuilder sb = new StringBuilder("Hi"); [Link](" Java");
3. Collections Framework
Q6. Difference between List, Set, and Map:
List – ordered, duplicates; Set – unordered, no duplicates; Map – key-value pairs.
Q7. HashMap vs TreeMap:
HashMap – unordered, allows null, faster; TreeMap – sorted, no nulls, slower.
Q8. Internal working of HashMap:
Uses hashCode() to locate bucket; collisions resolved using linked list or tree (Java 8+).
4. Exception Handling
Q9. Checked vs Unchecked Exceptions:
Checked – compile-time (IOException); Unchecked – runtime (NullPointerException).
Q10. throw vs throws:
throw – used to throw exception; throws – declares exception.
Q11. Example:
void read() throws IOException { throw new IOException("error"); }
5. Multithreading
Q12. Creating a Thread:
1. Extend Thread
2. Implement Runnable
Q13. start() vs run():
start() creates new thread; run() executes in same thread.
Q14. Synchronization:
Ensures one thread accesses a shared resource at a time using synchronized keyword.
6. Memory Management & Garbage Collection
Q15. Java Memory Areas:
Stack – method calls/local vars; Heap – objects.
Q16. Garbage Collection:
Automatic cleanup of unused objects.
Q17. Request GC:
[Link](); // request JVM
7. Java 8 Features
Q18. Major Features: Lambda, Stream API, Optional, Default methods.
Q19. Lambda Expression:
(x, y) -> x + y;
Q20. Stream Example:
[Link]().filter(x -> x > 10).forEach([Link]::println);
8. Design Patterns
Q21. Singleton Pattern:
Ensures single instance of class.
Example:
class Singleton { private static Singleton obj; private Singleton(){} public static Singleton
getInstance(){ if(obj==null)obj=new Singleton(); return obj;} }
Q22. DAO Pattern:
Separates data access from business logic using Data Access Object class.
9. Miscellaneous
Q23. == vs equals():
== checks reference; equals() checks content.
Q24. final, finally, finalize():
final – constant, finally – block in exception, finalize() – before GC.
Q25. volatile:
Ensures variable visibility across threads.
Q26. transient:
Prevents serialization of a field.