0% found this document useful (0 votes)
16 views4 pages

? Top 20 Scenario-Based Java Interview Questions

The document lists 20 scenario-based Java interview questions along with concise answers. Topics covered include handling exceptions, implementing design patterns, ensuring thread safety, and managing resources. It serves as a guide for candidates preparing for Java interviews by addressing common technical challenges and solutions.
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)
16 views4 pages

? Top 20 Scenario-Based Java Interview Questions

The document lists 20 scenario-based Java interview questions along with concise answers. Topics covered include handling exceptions, implementing design patterns, ensuring thread safety, and managing resources. It serves as a guide for candidates preparing for Java interviews by addressing common technical challenges and solutions.
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
You are on page 1/ 4

🚀 Top 20 Scenario-Based Java

Interview Questions
1. You have a large list of objects. How would you remove duplicates efficiently?

Answer:​
Use a Set like HashSet or LinkedHashSet to remove duplicates. If using custom objects,
override equals() and hashCode() properly.

2. How do you handle a NullPointerException in production code?

Answer:​
Avoid it with null checks, Optional, and defensive programming. Use tools like Lombok’s
@NonNull, and consider returning empty objects or collections.

3. How would you implement a thread-safe Singleton class in Java?

Answer:​
Use the Bill Pugh Singleton approach (static inner class) or Enum Singleton (which also
handles serialization and reflection).

4. A method throws a checked exception. How will you handle it in another method
without try-catch?

Answer:​
Propagate the exception using throws keyword in the calling method's signature.

5. How would you design a caching mechanism in Java?

Answer:​
Use HashMap for in-memory caching or frameworks like Caffeine, Ehcache, or Guava Cache.
Implement cache eviction (LRU, TTL) as needed.
6. How do you handle concurrent modification when iterating a collection?

Answer:​
Use Iterator and iterator.remove() or use CopyOnWriteArrayList /
ConcurrentHashMap depending on the structure. Avoid modifying inside enhanced for loop.

7. How would you find a memory leak in a Java application?

Answer:​
Use tools like VisualVM, jProfiler, or MAT (Memory Analyzer Tool). Look for unclosed
resources or static references holding objects.

8. How to create an immutable class in Java?

Answer:

●​ Declare class final​

●​ All fields private final​

●​ No setters​

●​ Deep copy in constructor and getter for mutable objects​

9. You have a list of employees. How do you sort by salary and then by name?

Answer:​
Use Java 8 Streams:

employees.stream()​
.sorted(Comparator.comparing(Employee::getSalary)​
.thenComparing(Employee::getName))​
.collect(Collectors.toList());
10. How to safely stop a thread in Java?

Answer:​
Use a volatile boolean flag like isRunning and check it inside the run loop. Avoid using
deprecated stop().

11. How to handle exceptions in a multi-threaded application?

Answer:​
Use Thread.setUncaughtExceptionHandler() for uncaught exceptions. For tasks via
ExecutorService, use Future.get() which throws ExecutionException.

12. How would you detect a deadlock in Java?

Answer:​
Use jconsole or jstack. Programmatically detect using
ThreadMXBean.findDeadlockedThreads().

13. How do you ensure resource cleanup (e.g., file, DB connection)?

Answer:​
Use try-with-resources (Java 7+), or in older code, try-finally blocks to ensure close()
is always called.

14. How to prevent method overriding in Java?

Answer:​
Declare the method as final. To prevent class inheritance altogether, mark the class as
final.

15. How do you compare two JSON strings in Java for logical equality?

Answer:​
Parse both JSONs to Map<String, Object> using libraries like Jackson or Gson, then
compare the maps.
16. How would you handle large file processing in Java?

Answer:​
Use BufferedReader with streaming, or Java NIO (Files.lines()), process line-by-line to
avoid OOM errors.

17. How would you implement retry logic in Java?

Answer:​
Use a loop with a retry counter and sleep (exponential backoff). Or use libraries like
Resilience4j or Spring Retry.

18. How do you ensure immutability in a multi-threaded environment?

Answer:​
Make the object immutable (final fields), no setters, use final class. No shared mutable state =
thread-safe.

19. You need to schedule a recurring task every 10 seconds. How?

Answer:​
Use ScheduledExecutorService or TimerTask. Prefer ScheduledExecutorService
for better control.

20. How would you design a producer-consumer solution?

Answer:​
Use BlockingQueue (e.g., ArrayBlockingQueue) to allow thread-safe message passing.
Producer puts, consumer takes.

You might also like