20 Commonly Asked Java Interview Questions and Answers

Interview QuestionsJava
  1. What is Java? 
    • Java is a high-level, object-oriented programming language designed to run on a variety of platforms.
  2. What are the features of Java? 
    • The main features of Java are platform independence, object-oriented programming, garbage collection, security, and multi-threading.
  3. What is the difference between an object and a class? 
    • A class is a blueprint or template for creating objects, while an object is an instance of a class.
  4. What is an interface in Java? 
    • An interface is a collection of abstract methods that can be implemented by any class.
  5. What is the difference between abstract classes and interfaces? 
    • An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.
  6. What is the purpose of the final keyword in Java? 
    • The final keyword can be used to make a variable, method or class immutable, which means they cannot be changed.
  7. What is the difference between static and non-static methods in Java? 
    • A static method can be accessed without creating an instance of a class, while a non-static method can only be accessed through an instance of a class.
  8. What is the purpose of the ‘this’ keyword in Java? 
    • The ‘this’ keyword refers to the current object and can be used to access its fields and methods.
  9. What is a constructor in Java? 
    • A constructor is a special method that is called when an object is created to initialize its state.
  10. What is inheritance in Java? 
    • Inheritance is a mechanism in which a new class is derived from an existing class, inheriting its properties and methods.
  11. What is polymorphism in Java? 
    • Polymorphism is the ability of an object to take on many forms, depending on the context in which it is used.
  12. What is the difference between a stack and a queue? 
    • A stack is a Last-In-First-Out (LIFO) data structure, while a queue is a First-In-First-Out (FIFO) data structure.
  13. What is a thread in Java? 
    • A thread is a lightweight process that can be used to achieve multi-tasking in a Java program.
  14. What is synchronization in Java? 
    • Synchronization is the process of coordinating access to shared resources to prevent race conditions and ensure consistency in a multi-threaded environment.
  15. What is exception handling in Java? 
    • Exception handling is a mechanism to handle errors and exceptions that may occur during the execution of a program.
  16. What is the difference between checked and unchecked exceptions in Java?
    • Checked exceptions are checked at compile time, while unchecked exceptions are not.
  17. What is the purpose of the ‘throw’ keyword in Java? 
    • The ‘throw’ keyword is used to explicitly throw an exception in a program.
  18. What is the purpose of the ‘finally’ block in Java? 
    • The ‘finally’ block is used to execute a block of code, regardless of whether an exception is thrown or not.
  19. What is the difference between a HashMap and a HashTable in Java? 
    • HashMap is not synchronized and allows null values, while HashTable is synchronized and does not allow null values.
  20. What is the purpose of the ‘super’ keyword in Java? 
    • The ‘super’ keyword is used to refer to the superclass of a class and can be used to call its constructors and methods.

Leave a Reply

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