Advanced Java Interview Questions and Answers (Part 2 of 2)
Explore more advanced and unique Java interview questions with clear explanations, continuing from part 1.
26. What is the difference between `==` and `Objects.equals()`?
`==` compares references, while `Objects.equals()` safely compares values and handles `null` without throwing `NullPointerException`.
27. What is a memory barrier in Java?
A memory barrier prevents instruction reordering by the compiler or processor, ensuring visibility and ordering guarantees in multithreaded environments.
28. What are phantom references in Java?
Phantom references are used to determine exactly when an object is removed from memory, with `ReferenceQueue` for post-cleanup actions.
29. How does Java handle method overloading and overriding?
Overloading is based on method signatures (compile-time), while overriding is based on runtime binding and class hierarchy.
30. What is type erasure in Java Generics?
Type erasure removes generic type information during compilation, ensuring backward compatibility with older Java versions.
31. Explain try-with-resources in Java.
Introduced in Java 7, it allows automatic closing of resources like streams or files that implement `AutoCloseable` or `Closeable`.
32. What is the significance of `instanceof` in Java?
`instanceof` checks if an object is of a particular type or subclass. From Java 14, pattern matching was introduced for cleaner syntax.
33. What is a deadlock and how can it be avoided?
Deadlock occurs when two or more threads wait indefinitely for each other’s resources. Avoid by consistent lock ordering and using timeouts.
34. What is method hiding in Java?
If a subclass defines a static method with the same signature as a static method in the parent class, it hides the method, not overrides it.
35. Explain the difference between `Collection` and `Collections` in Java.
`Collection` is an interface for data structures, while `Collections` is a utility class with static methods for collection operations.
36. What are records in Java?
Introduced in Java 14 (preview) and finalized in Java 16, records are immutable data carriers with concise syntax.
37. What is the purpose of the `sealed` keyword?
Introduced in Java 15, `sealed` classes restrict which other classes can extend or implement them using `permits`.
38. How are enums different from constants?
Enums are full-fledged classes with methods, fields, and constructors, offering type safety over traditional `static final` constants.
39. What are var handles?
Introduced in Java 9, VarHandles are a low-level mechanism for variable access and atomic operations, an alternative to `sun.misc.Unsafe`.
40. What is the use of the `assert` keyword?
`assert` is used to test assumptions in code and throw errors when false. It’s typically used during development and debugging.
41. What is the difference between `notify()` and `notifyAll()`?
`notify()` wakes a single waiting thread; `notifyAll()` wakes all waiting threads. Use carefully to prevent thread starvation or deadlocks.
42. What is escape analysis in Java?
Escape analysis determines the scope of object references. If an object does not escape a method, it may be allocated on the stack instead of the heap.
43. What are default methods and how do they work in multiple inheritance?
If multiple interfaces provide default methods, the implementing class must override to resolve conflicts explicitly.
44. What are the differences between Callable and Runnable?
`Callable` can return a result and throw a checked exception; `Runnable` does neither. Callable is preferred when results are needed.
45. What are method handles?
MethodHandles provide low-level, efficient access to methods, fields, and constructors, introduced in Java 7 under `java.lang.invoke` package.
46. What are memory-mapped files in Java?
Memory-mapped files use NIO to map a file directly into memory, improving I/O performance for large files.
47. Explain the difference between strong, soft, weak, and phantom references.
- Strong: Default, prevents GC.
- Soft: Cleared if memory is low.
- Weak: Cleared on next GC cycle.
- Phantom: Used for post-GC clean-up actions.
48. What is `ReentrantLock` and how is it different from `synchronized`?
`ReentrantLock` provides advanced locking control like tryLock, fairness, and interruption—features not available with `synchronized`.
49. What is the role of `ForkJoinPool` in Java?
ForkJoinPool supports parallelism by dividing tasks into subtasks (fork) and combining results (join), ideal for divide-and-conquer algorithms.
50. What is the `CompletableFuture` class?
Introduced in Java 8, `CompletableFuture` supports asynchronous programming, allowing chaining and combining of futures with callbacks.