0% found this document useful (0 votes)
6 views3 pages

Java Java 17032026 1b

The document outlines advanced Java interview questions and answers for candidates with over six years of experience. It covers topics such as volatile vs atomic vs synchronized, deadlock prevention, Stream API optimization, the Circuit Breaker pattern, handling production issues, and debugging memory leaks. Each section includes explanations, use cases, and code snippets to illustrate key concepts and best practices.

Uploaded by

Bhashin Bhash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Java 17032026 1b

The document outlines advanced Java interview questions and answers for candidates with over six years of experience. It covers topics such as volatile vs atomic vs synchronized, deadlock prevention, Stream API optimization, the Circuit Breaker pattern, handling production issues, and debugging memory leaks. Each section includes explanations, use cases, and code snippets to illustrate key concepts and best practices.

Uploaded by

Bhashin Bhash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like