Skip to main content

Java interview questions for experienced developers

1. What is the difference between == and equals() in Java?

  • Answer== is used to compare the reference (memory location) of two objects. It checks whether two references point to the same object in memory. equals() is used to compare the values of two objects. It is overridden in most classes (like String, Integer) to compare the contents of the objects rather than their memory locations.

2. Explain the concept of Garbage Collection in Java.

  • Answer: Java uses garbage collection to automatically manage memory. It identifies and removes objects that are no longer in use to free up memory resources. The Java Virtual Machine (JVM) has a built-in garbage collector that manages this process. Developers can invoke garbage collection using System.gc(), but there’s no guarantee when it will run.

3. What is the final, finally, and finalize() keyword in Java?

  • Answer:
    • final: Used to define constants, prevent method overriding, and inheritance.
    • finally: A block that is always executed after the try-catch block, even if an exception occurs.
    • finalize(): A method called by the garbage collector before an object is destroyed. It’s used to perform cleanup operations.

4. What is the difference between HashMap and Hashtable?

  • Answer:
    • HashMap is non-synchronized, meaning it is not thread-safe, while Hashtable is synchronized, making it thread-safe.
    • HashMap allows null keys and values, whereas Hashtable does not.
    • HashMap is generally faster as it is not synchronized, while Hashtable is slower due to the overhead of synchronization.

5. Explain the significance of transient keyword.

  • Answer: The transient keyword in Java is used to indicate that a variable should not be serialized when an object is being serialized. This is useful when you have sensitive or temporary data that should not be stored permanently.

6. What is the difference between StringBuilder and StringBuffer?

  • Answer:
    • StringBuffer is synchronized, meaning it is thread-safe. It should be used when multiple threads are accessing a single buffer.
    • StringBuilder is not synchronized, meaning it is faster but not thread-safe. It should be used when only one thread is modifying the string.
    • Both are mutable classes, but StringBuilder is preferred in single-threaded environments due to better performance.

7. What is the volatile keyword in Java?

  • Answer: The volatile keyword is used to indicate that a variable's value will be modified by different threads. It ensures visibility of changes to variables across threads. When a variable is declared as volatile, reads and writes to that variable are always performed from the main memory and not from the thread's local cache.

8. Explain the synchronized keyword and its use in Java.

  • Answer: The synchronized keyword in Java is used to lock a block of code or a method so that only one thread can execute it at a time. This is important in multithreaded environments to prevent data inconsistency.
    • There are two types of synchronization:
      • Method-level synchronization: Entire methods are synchronized using the synchronized keyword.
      • Block-level synchronization: Only a specific block of code is synchronized.

9. What are Java design patterns you have used in your projects?

  • Answer: Common design patterns include:
    • Singleton: Ensures that a class has only one instance and provides a global point of access.
    • Factory: Used to create objects without specifying the exact class of object that will be created.
    • Observer: A behavioral pattern where an object (subject) maintains a list of dependents (observers) and notifies them of any state changes.
    • Decorator: Adds behavior or functionality to an object dynamically without altering its structure.

10. What is the difference between Checked and Unchecked Exceptions?

  • Answer:
    • Checked Exceptions: These are exceptions that are checked at compile-time. The programmer is forced to handle these exceptions (e.g., IOException, SQLException).
    • Unchecked Exceptions: These are exceptions that are checked at runtime, such as NullPointerException or ArrayIndexOutOfBoundsException. They don’t need to be declared in a method's throws clause.

11. What is the difference between List, Set, and Map?

  • Answer:
    • List: An ordered collection (also known as a sequence). Elements can be accessed by their index, and duplicate elements are allowed (e.g., ArrayList, LinkedList).
    • Set: A collection that does not allow duplicate elements. Elements are unordered (e.g., HashSet, TreeSet).
    • Map: A collection that maps keys to values. A key cannot have duplicate values, but values can be duplicated (e.g., HashMap, TreeMap).

12. What is a Callable interface and how is it different from Runnable?

  • Answer: Callable and Runnable are both used to represent tasks that can be executed by threads, but they differ:
    • Runnable does not return a result and cannot throw a checked exception.
    • Callable returns a result (Future<T>) and can throw checked exceptions.

13. What is Java Stream API and how is it used?

  • Answer: The Stream API is introduced in Java 8 to process collections of objects in a functional programming style. It provides methods like map(), filter(), and forEach() to perform aggregate operations on data. Streams allow for parallel processing and are designed to be declarative, concise, and readable.

14. What is a Lambda Expression in Java?

Answer: Lambda expressions, introduced in Java 8, provide a concise way to represent an anonymous function. They allow you to pass functionality as an argument to methods and enable functional programming in Java. A typical lambda expression looks like this:
java code


   (parameters) -> expression

15. Explain the concept of Immutable objects.

  • Answer: Immutable objects are objects whose state cannot be changed after they are created. A classic example is the String class in Java. Once a String object is created, its value cannot be modified. Immutable objects are inherently thread-safe, as their state cannot change.

16. What are the four pillars of Object-Oriented Programming (OOP)?

  • Answer:
    1. Encapsulation: Wrapping data (variables) and code (methods) into a single unit (class), restricting access to some of the object's components.
    2. Abstraction: Hiding the complex implementation details and showing only the essential features of the object.
    3. Inheritance: Allowing one class to inherit the properties and behavior of another class.
    4. Polymorphism: The ability of an object to take many forms, typically achieved through method overriding or overloading.

These questions should help you prepare for interviews focusing on core Java concepts for experienced developers.