Java Concurrency: Mastering CompletableFuture vs Future
An advanced quiz exploring the differences and applications of CompletableFuture and Future in Java concurrency.
Questions in this quiz
10 questions on Java Concurrency: Mastering CompletableFuture vs Future. Work through them here, then start the quiz to answer them and see your score.
1.What is a key advantage of CompletableFuture over Future in Java concurrency?
- ACompletableFuture supports non-blocking asynchronous programming.
- BCompletableFuture can be used for blocking operations only.
- CCompletableFuture requires external libraries to function.
- DCompletableFuture is limited to single-threaded operations.
2.Which method in CompletableFuture allows chaining of multiple asynchronous tasks?
- Aget()
- Bcancel()
- CthenCompose()
- Djoin()
3.How does CompletableFuture handle exceptions compared to Future?
- ACompletableFuture requires try-catch blocks for exceptions.
- BCompletableFuture automatically logs exceptions.
- CCompletableFuture uses exceptionally() for exception handling.
- DCompletableFuture does not handle exceptions.
4.Which method would you use to wait for the completion of multiple CompletableFutures?
- AallOf()
- Bget()
- Cjoin()
- DanyOf()
5.What is the return type of CompletableFuture.supplyAsync()?
- Avoid
- BFuture<T>
- CCompletableFuture<T>
- DCompletableFuture<Void>
6.Which CompletableFuture method is used to combine two independent futures?
- AthenRun()
- BthenCombine()
- CthenAccept()
- DthenApply()
7.In CompletableFuture, what does the method thenApply() do?
- AIt logs the result of the future.
- BIt cancels the future if it is not completed.
- CIt blocks the current thread until the future completes.
- DIt applies a function to the result of the future upon completion.
8.What is a key difference between Future and CompletableFuture regarding task completion?
- AFuture allows chaining of tasks.
- BFuture can complete tasks asynchronously by default.
- CCompletableFuture is always blocking.
- DCompletableFuture can be manually completed using complete().
9.Which method in CompletableFuture is used to execute a callback after the future completes, regardless of the outcome?
- Ahandle()
- BwhenComplete()
- CthenAccept()
- DthenRun()
10.How does CompletableFuture improve performance over traditional Future?
- ABy reducing the need for exception handling.
- BBy simplifying the code syntax.
- CBy providing non-blocking operations and better resource utilization.
- DBy using more threads automatically.