Mastering Java Concurrency with CompletableFuture
An advanced quiz to assess your expertise in Java concurrency using CompletableFuture.
Questions in this quiz
10 questions on Mastering Java Concurrency with CompletableFuture. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary advantage of using CompletableFuture over traditional Future in Java?
- ACompletableFuture is easier to use with threads.
- BCompletableFuture allows non-blocking asynchronous programming.
- CCompletableFuture is faster than Future.
- DCompletableFuture can handle exceptions automatically.
2.Which method would you use to combine two CompletableFutures when both are completed?
- AthenApply()
- BthenCompose()
- CthenCombine()
- DthenAcceptBoth()
3.Consider the code: `CompletableFuture.supplyAsync(() -> "Hello").thenApply(s -> s + " World");`. What is the result?
- A"Hello World"
- Bnull
- C"World"
- D"Hello"
4.How can you handle exceptions in a CompletableFuture chain?
- Ausing exceptionally()
- Busing whenComplete()
- CAll of the above
- Dusing handle()
5.What does the method `CompletableFuture.anyOf()` return?
- AA CompletableFuture that completes when all are done.
- BA list of results from all CompletableFutures.
- CThe result of the last completed CompletableFuture.
- DThe result of the first completed CompletableFuture.
6.Which method allows you to execute a task asynchronously without returning a result?
- AthenAccept()
- BrunAsync()
- CsupplyAsync()
- DthenRun()
7.What is the purpose of the method `CompletableFuture.completedFuture()`?
- ATo create a CompletableFuture that is already completed with a given value.
- BTo complete a CompletableFuture manually.
- CTo create a CompletableFuture that never completes.
- DTo wait for the completion of a CompletableFuture.
8.Which method would you use to perform an action after a CompletableFuture completes, regardless of its outcome?
- AwhenComplete()
- BthenApply()
- Chandle()
- DthenAccept()
9.What does the method `thenCompose()` do in a CompletableFuture chain?
- ATransforms the result of a CompletableFuture.
- BExecutes a task after a CompletableFuture completes.
- CCombines two CompletableFutures into one.
- DFlattens two nested CompletableFutures.
10.Which of the following is true about `CompletableFuture.allOf()`?
- AIt cancels all CompletableFutures if one fails.
- BIt returns a list of results from all CompletableFutures.
- CIt returns a CompletableFuture that completes when all of the given CompletableFutures complete.
- DIt returns a CompletableFuture that completes when any of the given CompletableFutures complete.