Java Programming
Java Programming
June 9, 2025 at 07:33 PM
*Advanced Java Interview Questions with Answers*: *1. What is the Java Memory Model and how does it affect concurrency?* *Answer:* The Java Memory Model (JMM) defines how threads interact through memory and what behaviors are allowed in concurrent execution. It specifies rules about visibility of changes to variables across threads and ordering of operations to avoid issues like race conditions. Understanding JMM is crucial to write thread-safe code and use synchronization correctly. *2. Explain the difference between `synchronized` and `Lock` interface.* *Answer:* - `synchronized` is a keyword that locks an object’s monitor automatically. It’s simple but less flexible. - `Lock` interface (like `ReentrantLock`) provides more control (e.g., tryLock, interruptible lock acquisition), can be fair, and supports multiple condition variables. It requires explicit lock and unlock calls. *3. What is the volatile keyword and when should it be used?* *Answer:* `volatile` ensures visibility of changes to variables across threads without using locks. It guarantees reads/writes go directly to main memory, preventing caching issues. Use it for flags or simple state sharing but not for compound actions (like increment). *4. How does the `ForkJoinPool` work?* *Answer:* `ForkJoinPool` is a framework designed for parallelism by splitting tasks into smaller subtasks recursively (fork), then joining their results. It uses work-stealing algorithms to balance threads and optimize CPU usage. *5. What are Java’s garbage collection algorithms?* *Answer:* Common GC algorithms include: - Serial GC (single-threaded) - Parallel GC (multi-threaded, throughput focused) - CMS (Concurrent Mark-Sweep, low pause time) - G1 (Garbage-First, balances throughput and latency) - ZGC and Shenandoah (low-latency collectors introduced in recent JDKs) *6. Explain how class loading works in Java.* *Answer:* Java uses a delegation model: the Bootstrap ClassLoader loads core Java classes, then Extension ClassLoader, then Application ClassLoader. Custom loaders can be defined. Class loading involves loading, linking (verification, preparation, resolution), and initialization. *7. What is the difference between a thread pool and creating new threads?* *Answer:* Thread pools reuse existing threads to avoid overhead of thread creation, improving performance and resource management. Creating new threads each time is expensive and may cause resource exhaustion. *8. How do you prevent deadlocks?* *Answer:* - Acquire locks in a fixed global order. - Use tryLock with timeout. - Avoid nested locks when possible. - Use tools like thread dump analysis. *9. What are Java’s functional interfaces?* *Answer:* Interfaces with a single abstract method, used in lambda expressions and method references. Examples: `Runnable`, `Callable`, `Comparator`, `Function`, `Predicate`. *10. How does Java handle serialization?* *Answer:* Serialization converts an object’s state into a byte stream. Java uses `Serializable` interface marker and `ObjectOutputStream` / `ObjectInputStream`. Customize with `writeObject` and `readObject` methods. Be aware of serialVersionUID for version control. *React ❤️ for more*
❤️ 👍 🌸 🌹 👑 😢 64

Comments