20 Common Java interview questions and answers

Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and performance.

Here’s a list of 20 common Java interview questions that often come up:

1. What are the main features of Java?

Object-oriented
Platform-independent
Robust and Secure
Multithreaded
Architecture-neutral

2. Explain the difference between JDK, JRE, and JVM.

JDK (Java Development Kit) is for developing Java applications.
JRE (Java Runtime Environment) is for running Java applications.
JVM (Java Virtual Machine) is responsible for executing Java bytecode.

3. What is the difference between `==` and `.equals()` method in Java?

`==` is used to compare references (memory addresses) of objects.
`.equals()` is used to compare the contents or values of objects.

4. What is the difference between `ArrayList` and `LinkedList`?

`ArrayList` uses a dynamic array to store elements.
`LinkedList` uses a doubly linked list.
`ArrayList` is better for retrieving elements, `LinkedList` is better for adding/removing elements.

5. What is the `static` keyword in Java?

– `static` keyword is used to create variables and methods that belong to the class rather than instances of the class.
– `static` members can be accessed directly using the class name.

6. Explain the concept of inheritance in Java.

Inheritance allows one class (subclass) to inherit properties and methods from another class (superclass).
Helps in code reuse and method overriding.

7. What is polymorphism in Java?

Polymorphism allows methods to be written that can work with objects of different classes and exhibit different behaviors based on the object type.
Achieved through method overriding and method overloading.

8. What is encapsulation in Java?

Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data into a single unit (class).
Provides data hiding and protects the data from outside interference.

9. What are abstract classes and interfaces?

Abstract classes are classes that cannot be instantiated and are meant to be subclassed.
Interfaces are like abstract classes but can only contain method signatures and constants.
Java allows multiple inheritance through interfaces.

10. What is the difference between `final`, `finally`, and `finalize` in Java?

`final` keyword is used to declare constants, prevent method overriding, and prevent inheritance.
`finally` is used in exception handling to execute code irrespective of whether an exception is thrown or not.
`finalize` is a method called by the garbage collector before reclaiming the memory occupied by the object.

11. Explain the `try-catch-finally` block in Java.

`try` block is used to enclose the code that might throw an exception.
`catch` block is used to handle the exception that is thrown in the try block.
`finally` block is used to execute important code such as closing resources, irrespective of whether an exception is thrown or not.

12. What is the difference between checked and unchecked exceptions?

Checked exceptions are checked at compile-time and must be either caught or declared using `throws` keyword.
Unchecked exceptions are not checked at compile-time and typically result from programming errors (e.g., `NullPointerException`, `ArrayIndexOutOfBoundsException`).

13. What is a Java package and how is it used?

A package is a namespace that organizes a set of related classes and interfaces.
Helps in preventing naming conflicts and controls access.

14. What are the access modifiers in Java?

`private`: Accessible only within the same class.
`default` (no modifier): Accessible only within the same package.
`protected`: Accessible within the same package and by subclasses.
`public`: Accessible from anywhere.

15. What is the `this` keyword in Java?

`this` refers to the current instance of a class.
It is Used to differentiate between instance variables and parameters with the same name, and to invoke current class methods and constructors.

16. What are anonymous classes in Java?

Anonymous classes are local classes without a name, defined and instantiated in a single statement.
Often used for one-time use, particularly with interfaces.

17. What is the difference between `throw` and `throws` in Java?

`throw` is used to explicitly throw an exception.
`throws` is used in method signature to declare that a method may throw one or more exceptions.

18. What is the difference between `super` and `this` in Java?

`super` refers to the superclass instance of the current object.
`this` refers to the current instance of a class.

19. How does Java handle multithreading?

Java provides built-in support for multithreading through its `Thread` class and `Runnable` interface.
`synchronized` keyword and `java.util.concurrent` package provide additional mechanisms for thread synchronization and coordination.

20. What are lambda expressions in Java?

Lambda expressions introduce functional programming concepts to Java.
They provide a concise way to represent anonymous functions using syntax `(parameters) -> expression`.

These questions cover a broad range of topics typically discussed in Java interviews. It’s important to not only know the answers but also understand the underlying concepts and be able to explain them clearly.

Leave a Comment