Advanced Java: Mastering CompletableFuture
An advanced quiz exploring the intricacies of CompletableFuture in Java.
Questions in this quiz
10 questions on Advanced Java: Mastering 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?
- AIt provides a blocking mechanism to wait for results.
- BIt is easier to implement than Future.
- CIt allows non-blocking asynchronous programming.
- DIt is more memory efficient than Future.
2.Given the code snippet: `CompletableFuture.supplyAsync(() -> "Hello").thenApplyAsync(s -> s + " World")`, what will be the result?
- AA CompletableFuture that completes with "Hello"
- BA CompletableFuture that completes with "World"
- C"Hello World"
- DA CompletableFuture that completes with "Hello World"
3.How does CompletableFuture handle exceptions when using `handle` method?
- AIt allows handling exceptions and providing a fallback value.
- BIt ignores exceptions.
- CIt logs exceptions and continues execution.
- DIt propagates exceptions to the next stage.
4.What is the role of `CompletableFuture.allOf()` method?
- AIt returns the result of the first completed CompletableFuture.
- BIt completes when any of the given CompletableFutures complete.
- CIt cancels all given CompletableFutures.
- DIt completes when all of the given CompletableFutures complete.
5.Which method would you use to combine two CompletableFutures and process their results together?
- AthenApply()
- BthenCompose()
- CthenAcceptBoth()
- DthenCombine()
6.What is the difference between `thenApply()` and `thenCompose()` in CompletableFuture?
- A`thenApply()` is for combining results, `thenCompose()` is for splitting tasks.
- B`thenApply()` is for error handling, `thenCompose()` is for success handling.
- C`thenApply()` is for synchronous processing, `thenCompose()` is for chaining futures.
- D`thenApply()` is for chaining futures, `thenCompose()` is for synchronous processing.
7.How can you ensure that a CompletableFuture runs a task on a custom Executor?
- AUse `thenApplyAsync()` without parameters.
- BSet the Executor globally for all CompletableFutures.
- CCompletableFuture does not support custom Executors.
- DPass the Executor to `supplyAsync()` or `runAsync()`.
8.What happens if an exception is thrown in a CompletableFuture chain and not handled?
- AThe exception is swallowed silently.
- BThe CompletableFuture retries the operation.
- CThe CompletableFuture completes exceptionally.
- DThe exception is logged and execution continues.
9.Which method would you use to execute a CompletableFuture task only after another task completes successfully?
- AthenApply()
- BthenAccept()
- CthenCompose()
- DthenRun()
10.How can you retrieve the result of a CompletableFuture without blocking the main thread?
- AUse `get()` method.
- BUse `thenAccept()` to process the result asynchronously.
- CUse `getNow()` method.
- DUse `join()` method.