Birdeye - Top 20 Java Developer Interview Questions (0-3 Years Experience)


1. Explain the four pillars of Object-Oriented Programming (OOP).

Answer: The four pillars of OOP are:

  • Encapsulation: Wrapping data and methods into a single unit (class).
  • Inheritance: Mechanism where one class inherits the properties and behavior of another class.
  • Polymorphism: Ability of a class to take many forms, typically through method overriding and overloading.
  • Abstraction: Hiding implementation details and exposing only the essential features of an object.

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

Answer:

  • final: Used to declare constants, prevent method overriding, and prevent inheritance of classes.
  • finalize: A method in the Object class used for garbage collection before the object is destroyed.
  • finally: A block used for code that must be executed regardless of whether an exception occurs or not.

3. Explain the difference between `==` and `equals()` in Java.

Answer: `==` compares object references (memory locations), whereas `equals()` compares the actual content or state of the objects.

4. What is the difference between method overloading and method overriding?

Answer:

  • Overloading: Defining multiple methods with the same name but different parameter lists within the same class.
  • Overriding: Redefining a method in a subclass that already exists in the superclass, to provide a specific implementation.

5. What is polymorphism in Java, and how is it implemented?

Answer: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It is implemented through method overriding and method overloading.

6. What is the use of the `super` keyword in Java?

Answer: The `super` keyword is used to refer to the superclass of the current object. It is used to call superclass methods, constructors, or access superclass variables.

7. Can you explain Java's access modifiers (`private`, `protected`, `default`, and `public`)?

Answer:

  • private: Restricts access to the member within the same class.
  • protected: Allows access within the same package and subclasses.
  • default: Allows access only within the same package.
  • public: Allows access from anywhere.

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

Answer: `ArrayList` uses dynamic arrays to store elements, providing fast random access but slower insertions/removals. `LinkedList` uses a doubly-linked list, providing faster insertions/removals but slower access times.

9. What is a HashMap? How does it work internally?

Answer: A HashMap is a collection of key-value pairs. Internally, it uses an array and a hash function to compute the index of the key-value pairs. The hash function maps the keys to specific indices in the array.

10. Explain the differences between `HashSet` and `TreeSet`.

Answer: `HashSet` is an unordered collection with no duplicates, whereas `TreeSet` is a sorted collection that stores elements in a natural order or according to a custom comparator.

11. What is the difference between a `Stack` and a `Queue` in Java?

Answer:

  • Stack: Follows LIFO (Last In First Out) order, where the last element added is the first one to be removed.
  • Queue: Follows FIFO (First In First Out) order, where the first element added is the first one to be removed.

12. What is the difference between `ArrayList` and `Vector` in Java?

Answer: `ArrayList` is not synchronized, whereas `Vector` is synchronized. `Vector` also grows in size by doubling its capacity, while `ArrayList` grows by 50% when its capacity is exceeded.

13. What is synchronization in Java, and why is it important?

Answer: Synchronization ensures that a method or block of code can only be accessed by one thread at a time. It is important for avoiding race conditions when multiple threads are accessing shared resources.

14. What is a deadlock in Java, and how do you prevent it?

Answer: A deadlock occurs when two or more threads are blocked forever, waiting for each other to release resources. It can be prevented by avoiding circular dependencies, using timeout mechanisms, and locking resources in a specific order.

15. Explain Java's memory management and garbage collection process.

Answer: Java uses an automatic garbage collection process, which reclaims memory used by objects that are no longer referenced. It helps avoid memory leaks by freeing up memory, and it runs in the background.

16. What are the differences between `String`, `StringBuilder`, and `StringBuffer`?

Answer: `String` is immutable, whereas both `StringBuilder` and `StringBuffer` are mutable. `StringBuilder` is not synchronized, whereas `StringBuffer` is synchronized and thread-safe.

17. What is the `Optional` class in Java 8?

Answer: The `Optional` class is a container object which may or may not contain a value. It is used to avoid null pointer exceptions and helps in handling optional values in a more functional style.

18. What are functional interfaces in Java?

Answer: A functional interface is an interface with just one abstract method. It can have multiple default or static methods. Functional interfaces are used with lambda expressions and method references in Java 8.

19. What are the main features introduced in Java 8?

Answer: Java 8 introduced Lambda expressions, the Stream API, default methods in interfaces, and the `Optional` class, which allows for functional-style programming.

20. What is the difference between `Thread` and `Runnable` in Java?

Answer: `Thread` is a class, and `Runnable` is an interface. You can extend `Thread` to create a thread, or implement `Runnable` and pass it to a `Thread` object to execute the task in a separate thread.


Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad