Mastering Java's CompletableFuture and Asynchronous Programming
Test your advanced understanding of Java's CompletableFuture and asynchronous programming techniques.
Questions in this quiz
10 questions on Mastering Java's CompletableFuture and 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 traditional Future in Java?
- ACompletableFuture allows non-blocking operations.
- BCompletableFuture supports synchronous execution.
- CCompletableFuture is more memory efficient.
- DCompletableFuture is only used for I/O operations.
2.Which method would you use to combine two CompletableFutures and execute a callback when both complete?
- AthenAcceptBoth()
- BthenCompose()
- CthenCombine()
- DrunAfterBoth()
3.How does the exceptionally() method in CompletableFuture handle exceptions?
- AIt terminates the program.
- BIt provides a fallback result.
- CIt retries the task.
- DIt logs the exception.
4.Which method would you use to execute a task asynchronously and return a CompletableFuture?
- AexecuteAsync()
- BrunAsync()
- CstartAsync()
- DsupplyAsync()
5.What is the purpose of the thenCompose() method in CompletableFuture?
- ATo handle exceptions.
- BTo flatten nested CompletableFutures.
- CTo execute a task after a delay.
- DTo compose two independent tasks.
6.In a CompletableFuture, what does the method handle() do?
- AIt executes a task after completion.
- BIt logs the result of the task.
- CIt combines two CompletableFutures.
- DIt handles exceptions and provides a default value.
7.Which method in CompletableFuture is used to wait for multiple futures to complete?
- AallOf()
- BbothOf()
- CanyOf()
- DwaitForAll()
8.What is the difference between thenApply() and thenAccept() in CompletableFuture?
- AthenApply() returns a result, thenAccept() does not.
- BthenApply() is for exceptions, thenAccept() is not.
- CthenAccept() is for exceptions, thenApply() is not.
- DthenAccept() returns a result, thenApply() does not.
9.How can you ensure a CompletableFuture task runs on a specific Executor?
- ABy using a synchronized block.
- BBy using the default executor.
- CBy setting a global Executor.
- DBy passing the Executor to supplyAsync()
10.What is the role of the method completeExceptionally() in CompletableFuture?
- ATo ignore the exception and continue.
- BTo retry the task on failure.
- CTo log the exception.
- DTo complete the future with an exception.