Skip to content
Sharp

Concurrency basics interview questions

Threads, synchronization, volatile, atomics, executors.

110 questions with answers and explanations · 108 flashcards

Key ideas

  • count++ is read-modify-write and not atomic; unsynchronized threads lose updates.
  • synchronized gives mutual exclusion and visibility. volatile gives visibility and ordering, but not atomicity.
  • Prefer java.util.concurrent: AtomicInteger, LongAdder, ConcurrentHashMap, ExecutorService.
  • shutdown() stops accepting tasks and lets queued ones finish; shutdownNow() also interrupts running tasks.

Questions (page 2 of 3)

  1. How does ConcurrentHashMap ensure thread safety when updating entries?Multiple choiceMedium
  2. What happens if multiple threads simultaneously call incrementAndGet on an AtomicInteger?Multiple choiceHard
  3. Why is AtomicInteger preferred over synchronized blocks for counters in high-concurrency environments?Multiple choiceEasy
  4. How does AtomicReference enhance thread safety?Multiple choiceMedium
  5. Which method is used to atomically compute a new value for a key in a ConcurrentHashMap?Multiple choiceEasy
  6. Why do teams use CompletableFuture for asynchronous programming in production code?Multiple choiceEasy
  7. Which concurrency level of ConcurrentHashMap is the default in Java?Multiple choiceMedium
  8. Which of the following best describes the behavior of CompletableFuture.anyOf()?Multiple choiceHard
  9. In which scenario would using CompletableFuture.anyOf() be beneficial in an e-commerce flash sale?Multiple choiceHard
  10. Which package do AtomicInteger and AtomicReference belong to?Multiple choiceEasy
  11. How would you explain the expected behavior when multiple threads attempt to update the same key in a ConcurrentHashMap in an interview?Multiple choiceMedium
  12. Which statement about ConcurrentHashMap is FALSE?Multiple choiceEasy
  13. Which CompletableFuture method allows performing an action upon completion of a future without returning a result?Multiple choiceEasy
  14. In which scenario would a ConcurrentHashMap not provide a significant performance improvement over Collections.synchronizedMap?Multiple choiceHard
  15. Why does Compare-And-Swap (CAS) operations in atomic classes matter in this design?Multiple choiceMedium
  16. What potential issue should be considered when using AtomicInteger in a high-throughput application?Multiple choiceHard
  17. What would be the result of modifying the key of an entry in a ConcurrentHashMap during iteration?Multiple choiceHard
  18. What happens when a ConcurrentHashMap has more entries than the defined concurrency level?Multiple choiceMedium
  19. Why might Collections.synchronizedMap become a bottleneck in a high-concurrency application?Multiple choiceMedium
  20. In the context of Java's concurrency model, what role does 'volatile' play in the 'happens-before' relationship?Multiple choiceMedium
  21. How can you avoid deadlocks when using multiple ReentrantLocks in a method?Multiple choiceHard
  22. What does the 'volatile' keyword guarantee in Java?Multiple choiceEasy
  23. Why might using 'volatile' alone be insufficient for ensuring thread safety when updating a shared counter?Multiple choiceMedium
  24. Consider the following code snippet. What issue might occur if 'flag' is not declared as 'volatile'?Multiple choiceMedium
  25. When using synchronized blocks, how can you ensure fairness among threads?Multiple choiceHard
  26. How would you explain a common use case for 'volatile' variables in an interview?Multiple choiceEasy
  27. How can you ensure that a thread releases a lock if it was not able to acquire it using tryLock()?Multiple choiceMedium
  28. Given two methods with nested synchronized blocks on different objects, what scenario can lead to a deadlock?Multiple choiceHard
  29. Which of the following is NOT a property of a 'volatile' variable?Multiple choiceEasy
  30. How does the 'volatile' keyword affect the visibility of a variable across different CPU caches?Multiple choiceMedium
  31. Why does using 'synchronized' on a non-static method change runtime behavior?Multiple choiceEasy
  32. Given a scenario where multiple threads need to update a shared configuration object, why might 'volatile' not be sufficient to ensure thread safety?Multiple choiceHard
  33. What does the 'volatile' keyword ensure when applied to a variable in Java?Multiple choiceEasy
  34. Why does using synchronized on a method with respect to its class instance change runtime behavior?Multiple choiceMedium
  35. Why is it important for a 'volatile' variable to be written and read from main memory?Multiple choiceMedium
  36. When should you use the 'volatile' keyword?Multiple choiceMedium
  37. In the context of Java Concurrency, what is a critical section?Multiple choiceMedium
  38. Why is the 'volatile' keyword not sufficient for incrementing a shared counter?Multiple choiceMedium
  39. Which of the following best describes a critical section in Java concurrency?Multiple choiceMedium
  40. Which statement is true about intrinsic locks in Java?Multiple choiceEasy
  41. How does 'volatile' influence the Java Memory Model's ordering of operations?Multiple choiceHard
  42. In a system using multiple ReentrantLocks, how can you minimize the risk of deadlocks?Multiple choiceHard
  43. What will happen if two threads try to execute a synchronized method on the same object?Multiple choiceMedium
  44. How does the 'synchronized' keyword affect instance and static methods differently?Multiple choiceMedium
  45. In the context of Java Memory Model, what does 'happens-before' relationship mean?Multiple choiceMedium
  46. Which statement about the volatile keyword is true?Multiple choiceEasy
  47. How would you explain a common way to detect potential deadlocks in a Java application in an interview?Multiple choiceMedium
  48. Why should locks acquired using ReentrantLock be released in a finally block?Multiple choiceMedium
  49. Which of the following statements about lock fairness in ReentrantLock is true?Multiple choiceMedium
  50. Why is using nested synchronized blocks with different locks risky in practice?Multiple choiceEasy