Great—let’s continue and level this up into a complete senior (6+ years) interview pack
with deeper answers + real-world scenarios.
☕ JAVA (6+ YEARS) – ADVANCED +
SCENARIO BASED
🔹 7. Volatile vs Atomic vs Synchronized
Answer:
● volatile → ensures visibility, not atomicity
● synchronized → ensures mutual exclusion + visibility
● Atomic classes → lock-free thread-safe operations
When to use:
● volatile → flags
● synchronized → critical sections
● Atomic → counters, high-performance cases
Snippet:
class Counter {
private AtomicInteger count = new AtomicInteger(0);
public void increment() {
[Link]();
}
}
🔹 8. Deadlock Example + Prevention
Answer:
Deadlock occurs when 2 threads wait on each other’s locks.
Snippet:
Object lock1 = new Object();
Object lock2 = new Object();
Thread t1 = new Thread(() -> {
synchronized(lock1) {
synchronized(lock2) {}
}
});
Thread t2 = new Thread(() -> {
synchronized(lock2) {
synchronized(lock1) {}
}
});
Prevention:
● Lock ordering
● Timeout locks
● Avoid nested locks
🔹 9. Stream API Optimization
Answer:
Use parallel streams carefully (CPU-bound only)
Snippet:
List<Integer> list = [Link](1,2,3,4);
[Link]()
.filter(x -> x > 2)
.forEach([Link]::println);
🔹 10. Circuit Breaker Pattern
Answer:
Prevents cascading failures in microservices.
States:
● Closed → normal
● Open → block calls
● Half-open → test recovery
Example: using Resilience pattern
🔹 11. Production Issue Scenario
Question: API is slow in production—what do you do?
Answer Approach:
1. Check logs
2. Analyze thread dumps
3. Check DB queries
4. Monitor GC
5. Add caching
🔹 12. Memory Leak Debugging
Answer:
● Use heap dump
● Analyze with tools like MAT
● Common causes:
○ Static collections
○ Unclosed resources