Advanced Java Interview Questions and Answers (Part 1 of 2)

Explore 25 lesser-known but highly insightful Java interview questions with detailed answers to help you stand out.

1. What is the difference between `StringBuffer` and `StringBuilder`?

`StringBuffer` is thread-safe because its methods are synchronized. `StringBuilder` is faster but not thread-safe, making it ideal for single-threaded environments.

2. How does Java handle integer overflow?

Java does not throw an exception on overflow. Instead, it wraps around using 2’s complement arithmetic.

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

`==` checks for reference equality, while `.equals()` checks for value/content equality (can be overridden in classes).

4. Explain the concept of marker interfaces in Java.

A marker interface does not have methods. It’s used to provide metadata to the JVM or frameworks (e.g., `Serializable`).

5. What are transient variables in Java?

`transient` variables are not serialized during object serialization. They are skipped when writing the object to a stream.

6. Can we override a private or static method in Java?

No. Private methods are not visible to subclasses, and static methods belong to the class, not an instance, so they cannot be overridden.

7. What is the use of the `volatile` keyword in Java?

`volatile` ensures that changes to a variable are always visible to all threads, preventing the use of thread-local caches.

8. Difference between `final`, `finally`, and `finalize()`?

  • final: keyword to define constants, prevent method overriding or inheritance.
  • finally: block used for clean-up code in exception handling.
  • finalize(): method invoked by GC before object is destroyed (deprecated in newer versions).

9. How does garbage collection work in Java?

Java uses automatic garbage collection to reclaim memory from unreachable objects using various algorithms like Mark and Sweep, CMS, and G1.

10. What is the difference between heap and stack memory in Java?

Heap is used for dynamic memory allocation (objects), while stack holds method frames and local variables, operating in LIFO order.

11. What is the default size of an ArrayList?

The default initial capacity of an ArrayList is 10. It resizes dynamically by increasing capacity by 50% each time it exceeds.

12. What is method reference in Java 8?

Method reference is a shorthand for lambda expressions calling a specific method (e.g., `System.out::println`).

13. What is the diamond problem in Java and how does Java resolve it?

Java avoids the diamond problem in multiple inheritance by not supporting multiple inheritance with classes, only with interfaces.

14. What is a functional interface?

An interface with exactly one abstract method. Common examples include `Runnable`, `Callable`, and Java 8’s `Function`.

15. What is the purpose of `Optional` in Java 8?

`Optional` is a container object to represent null values and avoid `NullPointerException`. It encourages functional programming practices.

16. What is the use of the `default` keyword in interfaces?

Java 8 introduced `default` methods in interfaces to allow adding new methods without breaking implementing classes.

17. How does Java achieve platform independence?

Java source code is compiled into bytecode (.class), which is executed by the JVM on any platform with a compatible JVM.

18. What is autoboxing and unboxing?

Autoboxing converts primitives to wrapper objects automatically. Unboxing is the reverse process. E.g., `int` to `Integer`.

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

Checked exceptions are checked at compile-time and must be handled. Unchecked exceptions occur at runtime and are subclasses of `RuntimeException`.

20. Can a constructor be private in Java?

Yes. Private constructors are used in Singleton patterns or to restrict object creation (e.g., utility classes).

21. What is the difference between `HashMap` and `ConcurrentHashMap`?

`HashMap` is not thread-safe. `ConcurrentHashMap` allows concurrent access and uses internal segment-level locking for better concurrency.

22. Explain the concept of memory leaks in Java.

Memory leaks happen when objects are no longer needed but still referenced, preventing garbage collection.

23. What is the role of the `ClassLoader` in Java?

The `ClassLoader` loads classes into the JVM at runtime. Types include Bootstrap, Extension, and Application ClassLoaders.

24. What is the difference between `Path` and `File` in Java NIO?

`Path` is part of `java.nio.file`, more flexible and powerful than `File`, with better support for symbolic links and file system operations.

25. What is the purpose of the `synchronized` keyword?

`synchronized` ensures that only one thread can access a block or method at a time, helping avoid race conditions.

Stay tuned for Part 2: Questions 26–50, covering concurrency, JVM internals, Java 17 features, and more!

Leave a Reply

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

Copyright © 2022 - 2025 itfreesource.com