Java Interview Questions and Answers
Q1: What are the key features of Java?
A: Java is an object-oriented, platform-independent, secure, robust, multithreaded,
high-performance, and distributed programming language.
Q2: 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.
Q3: What is the difference between == and .equals() in Java?
A: - == compares memory addresses (reference comparison).
- .equals() compares the content of objects (logical comparison).
Q4: What are primitive data types in Java?
A: Java has 8 primitive data types: byte, short, int, long, float, double, char, and boolean.
Q5: 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).
Q6: 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).
Q7: 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).
Q8: What are wrapper classes in Java?
A: Wrapper classes provide an object representation of primitive types (e.g., Integer, Double,
Boolean).
Q9: 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.
Q10: 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.
Q11: What are the four principles of OOP?
A: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Q12: 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.
Q13: 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.
Q14: What is multiple inheritance in Java? Can we achieve it?
A: Java does not support multiple inheritance with classes but supports it with interfaces.
Q15: What is polymorphism? Provide an example.
A: Polymorphism allows a method to have multiple implementations, e.g., method overloading and
overriding.
Q16: 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.
Q17: How does garbage collection work in Java?
A: Garbage Collection (GC) automatically removes unreferenced objects from memory using
different algorithms like Mark-and-Sweep.
Q18: What are constructors? Can constructors be private?
A: Constructors initialize objects. Yes, constructors can be private, used in Singleton design
patterns.
Q19: 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.
Q20: What is multithreading in Java?
A: Multithreading allows concurrent execution of multiple threads for better performance.
Q21: What are synchronized methods in Java?
A: Synchronized methods prevent concurrent access to critical sections of code.
Q22: What is the difference between HashMap and HashTable?
A: - HashMap is non-synchronized, allows one null key, and is faster.
- HashTable is synchronized and does not allow null keys.
Q23: What is a lambda expression in Java?
A: Lambda expressions provide a concise way to implement functional interfaces.
Q24: What are Streams in Java 8?
A: Streams process data in a functional style with methods like filter, map, and reduce.
Q25: What is the default method in an interface?
A: A default method has a body in an interface and can be overridden.
Q26: What is a functional interface?
A: A functional interface has exactly one abstract method and can be used with lambda
expressions.
Q27: What are Optional classes in Java 8?
A: Optional helps handle null values without NullPointerException.
Q28: What is the difference between checked and unchecked exceptions?
A: - Checked exceptions are checked at compile-time (IOException, SQLException).
- Unchecked exceptions are runtime exceptions (NullPointerException, ArithmeticException).
Q29: What is dependency injection in Spring?
A: Dependency Injection (DI) allows injecting dependencies rather than creating them manually.
Q30: What is a Spring Boot starter?
A: Starters are pre-configured dependencies to simplify project setup.
Q31: What is Spring Boot auto-configuration?
A: Auto-configuration automatically configures beans based on dependencies.
Q32: What is the difference between @Component, @Service, and @Repository?
A: - @Component: Generic stereotype annotation.
- @Service: Specialized for service layer logic.
- @Repository: Used for DAO layers and integrates with JPA.
Q33: What is Hibernate in Java?
A: Hibernate is an ORM framework that simplifies database interactions using Java objects.
Q34: What is the difference between fetch types EAGER and LAZY in Hibernate?
A: - EAGER: Loads related entities immediately.
- LAZY: Loads related entities only when accessed.