Q101. How does the JVM handle method overloading and overriding internally?
- Overloading: Resolved at compile-time based on method signature.
- Overriding: Resolved at runtime using dynamic method dispatch based on the actual object type.
Q102. Explain Java’s Non-blocking Algorithms and how they differ from traditional blocking algorithms.
- Non-blocking algorithms avoid locks, enabling higher concurrency.
- Blocking algorithms rely on locks, risking deadlocks and reduced performance.
- Non-blocking solutions often use CAS (Compare-And-Swap).
Q103. How can you create an immutable class in Java?
- Use final for the class and all fields.
- Avoid setters.
- Provide deep copies for mutable objects.
- Do not expose internal states.
Q104. Describe the contract of hashCode() and equals() methods.
- If equals() returns true, hashCode() must return the same value.
- Two equal hash codes don’t imply objects are equal.
- Required for correct behavior in HashMap, HashSet.
Q105. What is Type Inference in Generics?
Type inference lets the compiler infer generic types during method calls, enhancing code readability and reducing boilerplate, especially with methods like Collections.emptyList() or diamond operator <>.
Q106. Explain the inner workings of ConcurrentHashMap.
- Java 7: Used segmented locking.
- Java 8+: Uses bucket-level locks + CAS operations.
- Reads are lock-free, enhancing speed.
- Great for multi-threaded applications and atomic operations like putIfAbsent().
Q107. What is Java Memory Fence and why is it important?
- Ensures correct memory operation ordering.
- Prevents CPU/compiler reordering.
- Used in volatile variables, locks, and ensures thread visibility.
Q108. Explain the concept of False Sharing in Java.
- Happens when threads access different variables in the same cache line.
- Leads to performance degradation due to frequent cache invalidations.
Q109. How do you handle OutOfMemoryError in Java?
- Optimize memory usage.
- Increase heap size (-Xmx).
- Fix memory leaks.
- Use efficient data structures.
- Apply GC tuning and fail-safe mechanisms.
Q110. Describe Escape Analysis in Java optimizations.
- JVM determines whether an object escapes a method.
- If not, it can be allocated on the stack instead of the heap.
- Reduces GC pressure and boosts performance.
Q111. What is Thread Starvation and how to prevent it?
- Occurs when low-priority threads are blocked indefinitely.
- Preventive measures:
- Use fair locks.
- Avoid priority inversion.
- Ensure balanced resource allocation.
- Use fair locks.
Q112. How does Class Data Sharing (CDS) work in JVM?
- Reduces startup time and memory usage.
- JVM preloads classes and stores metadata in a shared archive.
- Enables sharing across multiple JVM instances.
Q113. What is Thread-Local Allocation Buffer (TLAB)?
- Each thread gets a small chunk of memory in the Young Generation.
- Fast object allocation without locking.
- Enhances GC performance and thread efficiency.
Q114. How does the Fork/Join Framework work?
- Uses divide-and-conquer to split tasks (fork()).
- Uses ForkJoinPool for execution.
- Combines results with join().
- Supports work-stealing for load balancing.
Q115. Difference between Pessimistic and Optimistic Locking?
- Pessimistic Locking: Locks data immediately.
- Optimistic Locking: Assumes no conflict, verifies before commit.
- Optimistic is preferred in low-contention scenarios.
Q116. Explain the inner workings of a Java Agent.
- Uses java.lang.instrument package.
- Can modify bytecode at runtime or load-time.
- Used for profiling, monitoring, and instrumentation.
Q117. What are the uses of the Java Compiler API?
- Dynamically compile Java code (javax.tools.JavaCompiler).
- Useful in:
- IDEs.
- Plugin systems.
- Testing tools.
- Code generation frameworks.
- IDEs.
Q118. What is the Principle of Locality in Java?
- Programs reuse nearby memory locations frequently.
- Java leverages this for efficient CPU caching and memory access.
Q119. How does the G1 Garbage Collector work?
- G1 GC splits the heap into regions.
- Prioritizes regions with most garbage for collection.
- Offers predictable pause times and better performance tuning.
Q120. Explain Polymorphic Inline Caching (PIC).
- JVM caches method calls for polymorphic types.
- Optimizes virtual method dispatch in JIT.
- Improves CPU performance by reducing lookups.
Boost your Java interview preparation with these intermediate-level Java interview questions and answers. Perfect for developers aiming to master concurrency, JVM tuning, Java 8 features, and advanced Java concepts. This list covers StampedLock vs ReentrantLock, Project Loom, Java Modules, and more!
👉The Next 20 Questions-V: JAVA