Java Interview Questions and Answers
1. What is the difference between JDK, JRE, and JVM?
Answer: JDK (Java Development Kit) includes tools to develop Java programs, JRE (Java Runtime
Environment) includes JVM + libraries to run Java programs, and JVM (Java Virtual Machine) executes Java
bytecode.
2. What are the four principles of OOP?
Answer: Encapsulation, Abstraction, Inheritance, and Polymorphism.
3. What is a constructor in Java?
Answer: A constructor is a special method called when an object is instantiated. It initializes the object.
4. What is the use of 'this' keyword in Java?
Answer: 'this' refers to the current object of a class.
5. What is the difference between static and non-static methods?
Answer: Static methods belong to the class and don't require an object; non-static methods require an object
to be called.
6. What is method overloading?
Answer: Defining multiple methods with the same name but different parameters within the same class.
7. What is method overriding?
Answer: Redefining a method of the parent class in the child class.
8. What are access modifiers in Java?
Java Interview Questions and Answers
Answer: They define visibility: public, private, protected, and default (no modifier).
9. Difference between String, StringBuilder, and StringBuffer?
Answer: String is immutable, StringBuilder is mutable and not thread-safe, StringBuffer is mutable and
thread-safe.
10. What is a final variable, method, or class?
Answer: final variable = constant, final method = can't be overridden, final class = can't be inherited.
11. What is 'super' keyword?
Answer: Used to refer to the parent class's methods or constructors.
12. What is try-catch-finally?
Answer: Used for exception handling. 'finally' always executes.
13. What is a checked vs unchecked exception?
Answer: Checked: checked at compile time; Unchecked: runtime exceptions.
14. What are Collections in Java?
Answer: A framework to store and manipulate groups of data objects.
15. Difference between List, Set, and Map?
Answer: List: ordered and allows duplicates, Set: unordered and no duplicates, Map: key-value pairs.
16. What is the difference between ArrayList and LinkedList?
Java Interview Questions and Answers
Answer: ArrayList: fast access, slow insert/delete; LinkedList: slow access, fast insert/delete.
17. What is a HashMap?
Answer: A map-based collection that stores key-value pairs. Keys are unique.
18. Difference between == and .equals()?
Answer: == compares references; .equals() compares values.
19. What is an interface in Java?
Answer: An interface defines a contract that classes can implement.
20. What is an abstract class?
Answer: A class that cannot be instantiated and can have both abstract and concrete methods.
21. What is inheritance?
Answer: A mechanism where one class inherits properties of another class.
22. What is polymorphism?
Answer: One function behaves differently based on input - method overloading and overriding.
23. What is encapsulation?
Answer: Wrapping variables and methods together in a class and restricting direct access.
24. What is abstraction?
Answer: Hiding internal details and showing only essential features.
Java Interview Questions and Answers
25. What is the difference between throw and throws?
Answer: 'throw' is used to throw an exception, 'throws' declares exceptions a method can throw.
26. What is the default value of an object reference?
Answer: null.
27. What is the purpose of garbage collection in Java?
Answer: To automatically deallocate memory used by unreferenced objects.
28. What is a package in Java?
Answer: A namespace for classes and interfaces.
29. Difference between public, private, protected, and default access?
Answer: public: everywhere, private: within class, protected: package + subclass, default: package only.
30. Can a class extend multiple classes?
Answer: No. Java does not support multiple inheritance using classes.