1. What is the difference between abstract classes and interfaces in Java?
Abstract classes can have both abstract and concrete methods, while interfaces can only have abstract methods (until Java 8, which introduced default and static methods). A class can implement multiple interfaces but only extend one abstract class.
2. How does Java handle memory leaks despite having garbage collection?
Memory leaks can still occur in Java when objects are unintentionally referenced, such as through static fields or poorly managed collections, preventing the garbage collector from reclaiming memory.
3. What is the difference between fail-fast and fail-safe iterators?
Fail-fast iterators throw a `ConcurrentModificationException` if the collection is modified during iteration. Fail-safe iterators operate on a cloned copy, allowing safe iteration even when the original collection is modified.
4. Explain the use of the `Optional` class in Java 8.
`Optional` is a container object used to avoid `NullPointerException`. It represents a value that may or may not be present and provides methods like `isPresent()`, `ifPresent()`, and `orElse()`.
5. What is the difference between `Callable` and `Runnable`?
`Runnable` does not return a result or throw checked exceptions. `Callable` can return a result and throw checked exceptions, and is used with `ExecutorService` to retrieve results via `Future`.
6. How does the `volatile` keyword differ from `synchronized`?
`volatile` ensures visibility of changes to variables across threads but does not guarantee atomicity. `synchronized` provides both visibility and atomicity by locking critical sections.
7. What is the difference between `HashSet` and `TreeSet`?
`HashSet` is unordered and offers constant-time performance. `TreeSet` maintains elements in sorted order and uses a Red-Black tree, resulting in O(log n) time complexity.
8. What is the use of `transient` and `static` keywords in serialization?
`transient` fields are skipped during serialization. `static` fields belong to the class, not the instance, and are also not serialized.
9. What is the difference between `equals()` and `hashCode()`?
`equals()` checks logical equality, while `hashCode()` returns an integer used in hashing. If two objects are equal, they must have the same hash code.
10. What is the Java Memory Model (JMM)?
The JMM defines how threads interact through memory and ensures visibility and ordering of variables. It governs synchronization and volatile behavior.
11. What is the difference between `wait()`, `notify()`, and `notifyAll()`?
`wait()` causes the current thread to wait until another thread invokes `notify()` or `notifyAll()` on the same object. `notify()` wakes one waiting thread, while `notifyAll()` wakes all.
12. What is the role of the `final` keyword in Java?
`final` can be used with variables (to make them constants), methods (to prevent overriding), and classes (to prevent inheritance).
13. How does Java handle exception propagation?
Exceptions propagate up the call stack until caught by a matching `catch` block. If uncaught, they terminate the program.
14. What is the difference between `throw` and `throws`?
`throw` is used to explicitly throw an exception. `throws` is used in method signatures to declare exceptions that might be thrown.
15. What is the use of `ThreadLocal` in Java?
`ThreadLocal` provides thread-local variables, ensuring each thread has its own independent copy, useful for avoiding shared state issues.
16. What is the difference between `deep copy` and `shallow copy`?
A shallow copy copies object references, while a deep copy creates new instances of referenced objects, duplicating the entire object graph.
17. What is the purpose of the `enum` type in Java?
`enum` defines a fixed set of constants. It is type-safe and can include fields, methods, and constructors.
18. What are method references in Java 8?
Method references are shorthand for lambda expressions that call a method. Syntax: `ClassName::methodName`.
19. What is the difference between `Stream` and `ParallelStream`?
`Stream` processes elements sequentially, while `ParallelStream` splits the workload across multiple threads for parallel execution.
20. What is the use of `CompletableFuture`?
`CompletableFuture` is used for asynchronous programming. It allows chaining and combining multiple futures with callbacks.
21. What is the difference between `SoftReference`, `WeakReference`, and `PhantomReference`?
These are reference types used for memory-sensitive caching. `SoftReference` is cleared when memory is low, `WeakReference` is cleared during GC, and `PhantomReference` is used for post-mortem cleanup.
22. What is the use of `java.util.concurrent` package?
This package provides concurrency utilities like thread pools, executors, semaphores, and concurrent collections to simplify multithreaded programming.
23. What is the difference between `synchronized` block and method?
A synchronized method locks the entire method, while a synchronized block allows finer control by locking only a specific section of code or object.
24. What is the role of `ClassLoader` in Java?
`ClassLoader` loads classes into memory at runtime. It follows a delegation model: Bootstrap → Extension → Application class loader.
25. What is the difference between `instanceof` and `isInstance()`?
`instanceof` is a keyword used for compile-time type checking. `isInstance()` is a method of the `Class` class used for runtime type checking.
Search
Categories
Recent Posts
Top Git interview Questions for Experienced Developer
Speed-up tips and tricks for Windows 11
Top 15 Linux Commands used by Experienced Developer
Top SQL Interview Examples for Experienced Developer
Internal Working of Java HashMap
Recent Tags