20+ Java most asked interview questions with answers 2025

1. What is Java? Explain its features

Java is a high-level, object-oriented programming language developed by Sun Microsystems
(now Oracle) in 1995.
Key features:

  • Platform Independent: Write Once, Run Anywhere (WORA).
  • Object-Oriented: Follows OOP principles like encapsulation and inheritance.
  • Robust: Strong memory management and exception handling.
  • Multithreaded: Supports concurrent execution of threads.
  • Secure: No explicit pointers and runs in a virtual machine.

2. What are the main principles of Object-Oriented Programming (OOP)?

  1. Encapsulation: Wrapping data and methods in a single unit (class).
  2. Abstraction: Hiding implementation details and showing only the functionality.
  3. Inheritance: Allowing a class to inherit properties and methods from another class.
  4. Polymorphism: Using a single interface to represent different forms (overloading and
    overriding).

3. Differentiate between JDK, JRE, and JVM

  • JDK (Java Development Kit): Provides tools for development (compiler, debugger).
  • JRE (Java Runtime Environment): Includes libraries and JVM for running Java
    applications.
  • JVM (Java Virtual Machine): Converts bytecode into machine code and executes it.

4. Explain the concept of platform independence in Java

Java programs are compiled into bytecode, which is platform-independent. Bytecode is
executed by the JVM, which is platform-specific, ensuring the same Java program runs on
any OS with a compatible JVM.

5. What is the significance of the main method in Java?

The main method is the entry point of a Java application. Its signature is:
public static void main(String[] args)

  • public: Accessible globally.
  • static: Allows the JVM to call it without object instantiation.
  • void: Returns no value.
  • String[] args: Accepts command-line arguments.

6. How does Java achieve memory management?

Java uses automatic garbage collection to manage memory. Objects are allocated in the
heap memory, and when they are no longer referenced, the garbage collector deallocates
them.

7. What are constructors in Java? How are they different from methods?

  • Constructors: Special methods to initialize objects.
  • Name matches the class.
  • No return type.
  • Difference from methods: Methods perform actions; constructors initialize objects.

8. Explain method overloading and method overriding with examples.

  • Overloading: Same method name, different parameters (compile-time
    polymorphism).
    java
    CopyEdit
    class Example {
    void display(int a) { }
    void display(String b) { }
    }
  • Overriding: Subclass provides a new implementation for a method in the superclass
    (runtime polymorphism).
    java
    CopyEdit
    class Parent {
    void display() { }
    }
    class Child extends Parent {
    @Override
    void display() { }
    }

9. What is inheritance in Java? Discuss its types.

Inheritance allows a class to acquire the properties and methods of another class using the
extends keyword. Types:

  1. Single: One class inherits from another.
  2. Multilevel: A chain of inheritance.
  3. Hierarchical: Multiple classes inherit from one superclass.
  4. Multiple (via interfaces): A class implements multiple interfaces.

10. Define polymorphism and its types in Java.

Polymorphism allows methods to perform different tasks based on the object. Types:

  1. Compile-time (Method Overloading).
  2. Runtime (Method Overriding).

11. What is an interface in Java, and how does it differ from an abstract
class?

  • Interface: A collection of abstract methods and static constants.
  • Can have default and static methods (since Java 8).
  • A class can implement multiple interfaces.
    Difference:
  • Abstract class can have both abstract and concrete methods; an interface has abstract
    methods by default (Java 7 and below).
  • A class extends one abstract class but can implement multiple interfaces.

12. Describe the access modifiers in Java.

  • Public: Accessible everywhere.
  • Protected: Accessible within the same package and subclasses.
  • Default: Accessible within the same package only.
  • Private: Accessible within the same class only

13. What is encapsulation? How is it implemented in Java?

Encapsulation is bundling data (variables) and methods into a single unit (class). It’s
implemented using:

  1. Private access modifiers for fields.
  2. Public getter and setter methods for access.

14. Explain the concept of packages in Java.
Packages are namespaces used to group related classes and interfaces. They help avoid name
conflicts and improve organization.

15. What are static variables and methods? Provide examples.

  • Static Variable: Belongs to the class, shared by all objects.
  • Static Method: Can be called without creating an object of the class.

    class Example {
    static int count = 0; // Static variable
    static void display() { // Static method
    System.out.println(“Count: ” + count);
    }
    }

16. Discuss the lifecycle of a thread in Java.

  1. New: Thread is created.
  2. Runnable: Thread is ready to run.
  3. Running: Thread is executing.
  4. Blocked/Waiting: Thread is waiting for a resource.
  5. Terminated: Thread execution is complete

17. What is exception handling? How is it implemented in Java?
Exception handling manages runtime errors using try, catch, throw, throws, and
finally.

18. Differentiate between throw and throws keywords.

  • throw: Used to explicitly throw an exception.
  • throws: Declares exceptions a method might throw.

19. What are checked and unchecked exceptions?

  • Checked: Checked at compile-time (e.g., IOException).
  • Unchecked: Occur at runtime (e.g., NullPointerException).

20. Explain the concept of synchronization in Java.
Synchronization prevents thread interference by allowing only one thread to access a critical
section at a time, using the synchronized keyword.

21. What is the Java Collections Framework?
A unified architecture for storing and manipulating groups of objects, including interfaces
like List, Set, and Map.

22. Differentiate between ArrayList and LinkedList.

  • ArrayList: Backed by a dynamic array, faster for indexing.
  • LinkedList: Backed by a doubly-linked list, better for insertions/deletions.

23. What is a HashMap? How does it work internally?
HashMap stores key-value pairs using a hash table. Keys are hashed to determine the index,
and collisions are handled using linked lists or trees.

24. Explain the significance of the equals() and hashCode() methods.

  • equals(): Checks logical equality.
  • hashCode(): Provides a unique hash for an object, used in hash-based collections
    like HashMap

25. What is the difference between Comparable and Comparator?

  • Comparable: Used to define natural ordering.
  • Comparator: Defines custom ordering.

Leave a Reply

Your email address will not be published. Required fields are marked *