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 1 of 3)

  1. What does declaring a field volatile guarantee?Multiple choiceMedium
  2. What's the most accurate description of what this program prints?What does this code output?Medium
  3. Which changes would make the counter in a multi-threaded increment loop correct? Select all that apply.Select all that applyMedium
  4. Which ExecutorService method stops accepting new tasks but lets already-submitted tasks finish? (method name)Fill in the blankMedium
  5. Which method is used to wait for all CompletableFutures in a list to complete?Multiple choiceEasy
  6. How can you ensure that a CompletableFuture runs on a specific Executor?Multiple choiceHard
  7. Which situation benefits most from using AtomicInteger over synchronized blocks?Multiple choiceMedium
  8. Which CompletableFuture method should be used to execute a task asynchronously in a non-blocking manner?Multiple choiceMedium
  9. In which scenario would you use.thenCompose() over.thenApply() in CompletableFuture?Multiple choiceMedium
  10. Which CompletableFuture method should be used to handle both results and exceptions in a unified way?Multiple choiceMedium
  11. How does AtomicInteger's getAndIncrement method differ from incrementAndGet?Multiple choiceMedium
  12. How does AtomicReference help in implementing non-blocking algorithms?Multiple choiceMedium
  13. Which CompletableFuture method is used to combine the result of two independent futures?Multiple choiceEasy
  14. When using a ConcurrentHashMap, what happens if you attempt to insert a null value?Multiple choiceMedium
  15. When chaining CompletableFutures, which method allows you to perform an action without returning a result?Multiple choiceMedium
  16. What is one of the primary benefits of using ConcurrentHashMap in a multi-threaded environment?Multiple choiceEasy
  17. What will happen if an exception occurs in a CompletableFuture handled by.handle()?Multiple choiceMedium
  18. Which Java collection is designed for high concurrency and avoids global locking?Multiple choiceEasy
  19. Which CompletableFuture method allows you to execute a task asynchronously and return its result?Multiple choiceEasy
  20. Which CompletableFuture method can be used to process the result of a computation and return a new CompletableFuture?Multiple choiceMedium
  21. Which method should you use to ensure a CompletableFuture chain executes on a specified Executor?Multiple choiceEasy
  22. How can you handle exceptions in a CompletableFuture without terminating the chain?Multiple choiceMedium
  23. What does the.thenCombine() method do when used with two CompletableFutures?Multiple choiceMedium
  24. Which method of AtomicInteger performs a Compare-And-Swap (CAS) operation?Multiple choiceMedium
  25. Why might an AtomicInteger not be suitable for a counter in a distributed system?Multiple choiceHard
  26. What does the atomicity of AtomicInteger ensure?Multiple choiceEasy
  27. How does CompletableFuture handle exceptions that are thrown in asynchronous computations?Multiple choiceMedium
  28. Which of the following scenarios could cause a standard HashMap to enter an infinite loop?Multiple choiceMedium
  29. What could be a scenario where ConcurrentHashMap might not offer significant performance benefits over Collections.synchronizedMap?Multiple choiceHard
  30. Which of the following is a potential drawback of using lock striping in ConcurrentHashMap?Multiple choiceHard
  31. Why does the.thenApply() method in CompletableFuture matter in practice?Multiple choiceMedium
  32. Why does the CompletableFuture.join() method matter in practice?Multiple choiceMedium
  33. Why does the 'synchronized' keyword in Java matter in practice?Multiple choiceEasy
  34. How does ConcurrentHashMap handle null keys or values?Multiple choiceMedium
  35. In which scenario is using an AtomicReference not sufficient?Multiple choiceHard
  36. In a high-concurrency scenario, why might a ConcurrentHashMap still lead to performance issues?Multiple choiceHard
  37. Which method would you use to update an AtomicInteger only if it currently holds a specific value?Multiple choiceMedium
  38. Why might you choose CompletableFuture over traditional Future in Java?Multiple choiceMedium
  39. Why might a ConcurrentHashMap lead to less than ideal performance despite its concurrency features?Multiple choiceHard
  40. Why do teams use AtomicInteger over a synchronized block in production code?Multiple choiceEasy
  41. In which scenario might AtomicReference become insufficient for implementing a thread-safe data structure?Multiple choiceHard
  42. Why can using CompletableFuture with a large number of tasks in a high-load scenario become a problem in practice?Multiple choiceHard
  43. How does CompletableFuture handle a chain of future computations when an exception occurs?Multiple choiceHard
  44. Which of the following statements is true about AtomicInteger?Multiple choiceMedium
  45. In a multithreaded e-commerce application, why is it dangerous to use a standard HashMap for tracking session data?Multiple choiceEasy
  46. How does ConcurrentHashMap improve performance over Collections.synchronizedMap in a high-concurrency setting?Multiple choiceMedium
  47. In a high-traffic e-commerce application, how can you optimize the completion of multiple independent checkout tasks using CompletableFuture?Multiple choiceHard
  48. Why do teams use ConcurrentHashMap over a standard HashMap in a multi-threaded environment in production code?Multiple choiceEasy
  49. Given a ConcurrentHashMap, which method would you use to atomically update a value for a given key?Multiple choiceMedium
  50. Which class would you use for an atomic counter in Java?Multiple choiceEasy