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.synchronizedgives mutual exclusion and visibility.volatilegives 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)
- How does ConcurrentHashMap ensure thread safety when updating entries?Multiple choiceMedium
- What happens if multiple threads simultaneously call incrementAndGet on an AtomicInteger?Multiple choiceHard
- Why is AtomicInteger preferred over synchronized blocks for counters in high-concurrency environments?Multiple choiceEasy
- How does AtomicReference enhance thread safety?Multiple choiceMedium
- Which method is used to atomically compute a new value for a key in a ConcurrentHashMap?Multiple choiceEasy
- Why do teams use CompletableFuture for asynchronous programming in production code?Multiple choiceEasy
- Which concurrency level of ConcurrentHashMap is the default in Java?Multiple choiceMedium
- Which of the following best describes the behavior of CompletableFuture.anyOf()?Multiple choiceHard
- In which scenario would using CompletableFuture.anyOf() be beneficial in an e-commerce flash sale?Multiple choiceHard
- Which package do AtomicInteger and AtomicReference belong to?Multiple choiceEasy
- How would you explain the expected behavior when multiple threads attempt to update the same key in a ConcurrentHashMap in an interview?Multiple choiceMedium
- Which statement about ConcurrentHashMap is FALSE?Multiple choiceEasy
- Which CompletableFuture method allows performing an action upon completion of a future without returning a result?Multiple choiceEasy
- In which scenario would a ConcurrentHashMap not provide a significant performance improvement over Collections.synchronizedMap?Multiple choiceHard
- Why does Compare-And-Swap (CAS) operations in atomic classes matter in this design?Multiple choiceMedium
- What potential issue should be considered when using AtomicInteger in a high-throughput application?Multiple choiceHard
- What would be the result of modifying the key of an entry in a ConcurrentHashMap during iteration?Multiple choiceHard
- What happens when a ConcurrentHashMap has more entries than the defined concurrency level?Multiple choiceMedium
- Why might Collections.synchronizedMap become a bottleneck in a high-concurrency application?Multiple choiceMedium
- In the context of Java's concurrency model, what role does 'volatile' play in the 'happens-before' relationship?Multiple choiceMedium
- How can you avoid deadlocks when using multiple ReentrantLocks in a method?Multiple choiceHard
- What does the 'volatile' keyword guarantee in Java?Multiple choiceEasy
- Why might using 'volatile' alone be insufficient for ensuring thread safety when updating a shared counter?Multiple choiceMedium
- Consider the following code snippet. What issue might occur if 'flag' is not declared as 'volatile'?Multiple choiceMedium
- When using synchronized blocks, how can you ensure fairness among threads?Multiple choiceHard
- How would you explain a common use case for 'volatile' variables in an interview?Multiple choiceEasy
- How can you ensure that a thread releases a lock if it was not able to acquire it using tryLock()?Multiple choiceMedium
- Given two methods with nested synchronized blocks on different objects, what scenario can lead to a deadlock?Multiple choiceHard
- Which of the following is NOT a property of a 'volatile' variable?Multiple choiceEasy
- How does the 'volatile' keyword affect the visibility of a variable across different CPU caches?Multiple choiceMedium
- Why does using 'synchronized' on a non-static method change runtime behavior?Multiple choiceEasy
- 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
- What does the 'volatile' keyword ensure when applied to a variable in Java?Multiple choiceEasy
- Why does using synchronized on a method with respect to its class instance change runtime behavior?Multiple choiceMedium
- Why is it important for a 'volatile' variable to be written and read from main memory?Multiple choiceMedium
- When should you use the 'volatile' keyword?Multiple choiceMedium
- In the context of Java Concurrency, what is a critical section?Multiple choiceMedium
- Why is the 'volatile' keyword not sufficient for incrementing a shared counter?Multiple choiceMedium
- Which of the following best describes a critical section in Java concurrency?Multiple choiceMedium
- Which statement is true about intrinsic locks in Java?Multiple choiceEasy
- How does 'volatile' influence the Java Memory Model's ordering of operations?Multiple choiceHard
- In a system using multiple ReentrantLocks, how can you minimize the risk of deadlocks?Multiple choiceHard
- What will happen if two threads try to execute a synchronized method on the same object?Multiple choiceMedium
- How does the 'synchronized' keyword affect instance and static methods differently?Multiple choiceMedium
- In the context of Java Memory Model, what does 'happens-before' relationship mean?Multiple choiceMedium
- Which statement about the volatile keyword is true?Multiple choiceEasy
- How would you explain a common way to detect potential deadlocks in a Java application in an interview?Multiple choiceMedium
- Why should locks acquired using ReentrantLock be released in a finally block?Multiple choiceMedium
- Which of the following statements about lock fairness in ReentrantLock is true?Multiple choiceMedium
- Why is using nested synchronized blocks with different locks risky in practice?Multiple choiceEasy