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