---Advertisement---

Java Interview Questions and Answers (Part-V)

By Manisha

Published On:

---Advertisement---

Q121: What is the difference between StampedLock and ReentrantLock?

StampedLock vs ReentrantLock

  • Locking Mechanism:
    • StampedLock: Supports optimistic and pessimistic locking, improving read concurrency.
    • ReentrantLock: Provides traditional exclusive locking with reentrant capabilities.
  • Read Performance:
    • StampedLock: Allows optimistic reads without blocking, boosting speed.
    • ReentrantLock: Does not support optimistic reads; always requires locking.
  • Write Locking:
    • StampedLock: Offers exclusive write lock invalidating optimistic read stamps.
    • ReentrantLock: Standard exclusive lock with thread reentrancy.
  • Interruptibility:
    • StampedLock: Write locks are non-interruptible.
    • ReentrantLock: Supports interruptible locks, improving responsiveness.
  • Use Case:
    • StampedLock: Best for high-read, low-write scenarios (e.g., caches).
    • ReentrantLock: Suitable for general-purpose locking with fairness.

Q122: Explain the term “Busy Spin” in multi-threading.

Busy Spin is a technique where a thread continuously checks a condition in a tight loop instead of sleeping or blocking, reducing context-switch overhead but consuming CPU cycles.

  • Use Cases: Low-latency systems like high-frequency trading.
  • Drawbacks: High CPU usage; inefficient for long waits.
  • Better Alternatives: Use blocking methods like LockSupport.park() for longer waits.

Q123: How can you implement a custom ClassLoader?

Create a subclass of ClassLoader and override methods such as findClass(), loadClass(), and defineClass() to customize how Java classes are loaded at runtime.


Q124: Describe Java JIT Compiler optimizations like Loop Unrolling and Vectorization.

  • Loop Unrolling: Reduces loop control overhead by increasing the number of operations per iteration.
  • Vectorization: Converts scalar operations into parallel SIMD instructions to process multiple data elements simultaneously, improving CPU efficiency.

Q125: Explain the use of the jstack tool.

jstack prints the thread stack traces of a running JVM process, useful for debugging deadlocks, thread dumps, and performance bottlenecks.


Q126: Describe Java’s Project Loom and its impact on concurrency.

Project Loom introduces lightweight virtual threads to the JVM, enabling massive concurrency with millions of threads.

  • Benefits: Simplifies concurrency, reduces resource use, improves I/O-bound performance, and integrates smoothly with existing APIs.

Q127: What is a Java Decompiler, and how can it be used securely?

A Java Decompiler converts .class files back to readable Java source code.

  • Secure Use:
    • Avoid decompiling proprietary code.
    • Use for debugging and recovery.
    • Protect your code with obfuscators like ProGuard.
    • Use trusted tools (JD-GUI, CFR).

Q128: What are the challenges in implementing Distributed Garbage Collection (DGC)?

  • Tracking cross-JVM object references.
  • Handling network latency and failures.
  • Avoiding memory leaks.
  • Minimizing performance overhead.

Q129: Describe the impact of JVM flags on performance tuning.

JVM flags control heap size, garbage collection, thread management, and logging, directly affecting Java app performance.

  • Examples:
    • -Xms/-Xmx for heap size.
    • -XX:+UseG1GC for garbage collection tuning.
    • -XX:+PrintGCDetails for GC logging.

Q130: What are Java Modules, and how do they enhance security and maintainability?

Introduced in Java 9, modules enable encapsulation by exposing only selected packages, improving security, reducing classpath conflicts, and speeding up startup.


Q131: Explain the concept of Java 8 parallelStream().

parallelStream() enables parallel processing of collections using the ForkJoin framework, ideal for CPU-intensive tasks on multi-core processors, improving throughput.


Q132: What is a deadlock in Java?

Deadlock occurs when two or more threads are blocked forever, each waiting for a resource held by another. Proper lock ordering and timeout mechanisms can prevent deadlocks.


Q133: What is the difference between Executor and ExecutorService?

  • Executor: Basic interface to run asynchronous tasks (execute() method).
  • ExecutorService: Extends Executor with lifecycle management (shutdown, submit with results, etc.).

Q134: What is the difference between HashMap and Hashtable?

FeatureHashMapHashtable
SynchronizationNot synchronized (not thread-safe)Synchronized (thread-safe)
Null keys/valuesAllows one null key and multiple null valuesDoes not allow null keys or values
PerformanceFasterSlower due to synchronization

Q135: Explain Java 8 default methods in interfaces.

Java 8 introduced default methods allowing interfaces to provide method implementations, enabling backward compatibility and multiple inheritance of behavior.


Q136: What is the difference between wait() and notify()?

  • wait(): Releases the lock and puts thread to waiting state until notified.
  • notify(): Wakes up one waiting thread to continue execution.
  • Both require synchronization on the same object monitor.

Q137: What is the difference between Comparable and Comparator?

  • Comparable: Defines natural ordering via compareTo() inside the class.
  • Comparator: Defines custom ordering via compare() in a separate class.

Q138: What is the transient keyword in Java?

transient marks a field to be excluded from serialization, so it is not saved during object serialization.


Q139: What is the purpose of the instanceof operator?

instanceof checks if an object is an instance of a specific class or interface before type casting.


Q140: Explain the concept of lock-free programming in Java.

Lock-free programming avoids traditional locks, using atomic variables and CAS (Compare-And-Swap) operations to ensure thread-safe concurrency with higher performance and no blocking.

👉The Next 10 Questions-Ⅵ : JAVA

---Advertisement---

Leave a Comment