Java Interview Questions and Answers
Q: What are the key features of Java?
A: Java is an object-oriented, platform-independent, secure, robust, multithreaded,
high-performance, and distributed programming language.
Q: Explain the difference between JDK, JRE, and JVM.
A: - JDK (Java Development Kit): Includes tools for developing, debugging, and compiling Java
applications.
- JRE (Java Runtime Environment): Contains libraries and JVM to run Java applications.
- JVM (Java Virtual Machine): Converts Java bytecode to machine code for execution.
Q: What is the difference between == and .equals() in Java?
A: - == compares memory addresses (reference comparison).
- .equals() compares the content of objects (logical comparison).
Q: What are primitive data types in Java?
A: Java has 8 primitive data types: byte, short, int, long, float, double, char, and boolean.
Q: What is type casting in Java?
A: Type casting is converting one data type into another:
- Implicit Casting (Widening): Smaller to larger type (int to double).
- Explicit Casting (Narrowing): Larger to smaller type (double to int).
Q: What is autoboxing and unboxing?
A: - Autoboxing: Automatic conversion of primitive to Wrapper class (int to Integer).
- Unboxing: Automatic conversion of Wrapper class to primitive (Integer to int).
Q: What is the difference between String, StringBuilder, and StringBuffer?
A: - String: Immutable and thread-safe.
- StringBuilder: Mutable and not thread-safe (faster).
- StringBuffer: Mutable and thread-safe (slower).
Q: What are wrapper classes in Java?
A: Wrapper classes provide an object representation of primitive types (e.g., Integer, Double,
Boolean).
Q: Explain pass-by-value and pass-by-reference in Java.
A: Java is strictly pass-by-value. Primitive types pass values, while objects pass references to the
memory location.
Q: What is the difference between an interface and an abstract class?
A: - Interface: Defines only abstract methods (Java 8+ allows default and static methods).
- Abstract Class: Can have both abstract and concrete methods.
Q: What are the four principles of OOP?
A: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Q: What is method overloading and method overriding?
A: - Overloading: Defining multiple methods with the same name but different parameters.
- Overriding: Redefining a method in a subclass that exists in the parent class.
Q: What is the difference between final, finally, and finalize()?
A: - final: Used to declare constants, prevent method overriding, and inheritance.
- finally: A block in exception handling that always executes.
- finalize(): A method called by the garbage collector before an object is destroyed.
Q: What is multiple inheritance in Java? Can we achieve it?
A: Java does not support multiple inheritance with classes but supports it with interfaces.
Q: What is polymorphism? Provide an example.
A: Polymorphism allows a method to have multiple implementations, e.g., method overloading and
overriding.
Q: What is the difference between abstraction and encapsulation?
A: - Abstraction: Hiding implementation details and exposing only functionality.
- Encapsulation: Bundling data with methods to restrict direct access.
Q: How does garbage collection work in Java?
A: Garbage Collection (GC) automatically removes unreferenced objects from memory using
different algorithms like Mark-and-Sweep.
Q: What are constructors? Can constructors be private?
A: Constructors initialize objects. Yes, constructors can be private, used in Singleton design
patterns.
Q: What is the super keyword in Java?
A: super refers to the parent class. It is used to call a parent class constructor or methods.