Intermediate Java CompletableFuture & Asynchronous Programming
Test your knowledge on Java's CompletableFuture and asynchronous programming techniques.
Questions in this quiz
12 questions on Intermediate Java CompletableFuture & Asynchronous Programming. 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 Future in Java?
- ACompletableFuture is easier to use with threads.
- BCompletableFuture is non-blocking and supports chaining.
- CCompletableFuture is faster than Future.
- DCompletableFuture requires less memory.
2.Which method would you use to combine two CompletableFutures to run in parallel and process their results together?
- AthenCombine
- BthenApplyAsync
- CthenAcceptBoth
- DthenRun
3.Consider the code: `CompletableFuture.supplyAsync(() -> "Hello").thenApplyAsync(s -> s + " World")`. What does this code do?
- AExecutes asynchronously and returns "Hello World".
- BExecutes asynchronously and returns "Hello".
- CExecutes synchronously and returns "Hello".
- DExecutes synchronously and returns "Hello World".
4.Which method allows you to handle exceptions in a CompletableFuture chain?
- AcompleteExceptionally
- Bexceptionally
- Chandle
- DthenCatch
5.How can you ensure that a CompletableFuture completes with a default value if it times out?
- Ause the orTimeout method
- Buse the completeOnTimeout method
- Cuse the complete method
- Duse the exceptionally method
6.What does the method `thenAccept` do in a CompletableFuture?
- AIt transforms the result of a CompletableFuture.
- BIt combines two CompletableFutures.
- CIt handles exceptions in a CompletableFuture.
- DIt consumes the result of a CompletableFuture without returning a new CompletableFuture.
7.Which method would you use to execute a task asynchronously without returning a result?
- AsupplyAsync
- BthenRunAsync
- CthenApplyAsync
- DrunAsync
8.What is the purpose of the `join` method in CompletableFuture?
- ATo block and wait for the CompletableFuture to complete.
- BTo handle exceptions in the CompletableFuture.
- CTo execute a task asynchronously.
- DTo combine two CompletableFutures.
9.Which method would you use to execute a callback function when a CompletableFuture completes, regardless of its outcome?
- Ahandle
- BthenApply
- CwhenComplete
- DthenAccept
10.What is the difference between `thenApply` and `thenCompose` in CompletableFuture?
- AthenApply transforms the result; thenCompose flattens nested CompletableFutures.
- BthenApply is for synchronous operations; thenCompose is for asynchronous operations.
- CthenApply handles exceptions; thenCompose does not.
- DthenApply returns a CompletableFuture; thenCompose returns a value.
11.Which method would you use to cancel a CompletableFuture?
- Aabort
- BcompleteExceptionally
- Ccomplete
- Dcancel
12.What happens if you call `complete` on a CompletableFuture that has already completed?
- AIt resets the CompletableFuture.
- BIt completes the CompletableFuture again with a new value.
- CIt throws an IllegalStateException.
- DIt has no effect.