AI for Backend: Mastering Java CompletableFuture & Asynchronous Programming
An advanced quiz on Java CompletableFuture and asynchronous programming techniques.
Questions in this quiz
10 questions on AI for Backend: Mastering 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 traditional Future in Java?
- ACompletableFuture consumes less memory.
- BCompletableFuture is faster in executing tasks.
- CCompletableFuture can handle exceptions more gracefully.
- DCompletableFuture supports non-blocking operations and chaining.
2.Given the code snippet `CompletableFuture.supplyAsync(() -> "Hello").thenApplyAsync(s -> s + " World")`, what is the expected output?
- AHello
- BWorld
- CAn exception is thrown
- DHello World
3.Which method in CompletableFuture is used to handle exceptions that occur during asynchronous computation?
- AhandleAsync
- Bexceptionally
- CthenAccept
- DthenCombine
4.How does CompletableFuture.allOf() behave when one of the futures completes exceptionally?
- AIt cancels all remaining futures.
- BIt waits for all futures to complete and then completes exceptionally.
- CIt ignores the exception and completes normally.
- DIt completes exceptionally immediately.
5.What is the purpose of the `thenCompose` method in CompletableFuture?
- ATo flatten nested CompletableFutures.
- BTo handle exceptions in the computation.
- CTo execute a task after a future completes.
- DTo combine two independent futures.
6.In which scenario would you prefer `thenCombine` over `thenCompose`?
- AWhen you need to handle exceptions.
- BWhen you need to execute a task after a future completes.
- CWhen you want to flatten nested futures.
- DWhen you want to combine results of two independent futures.
7.What is the effect of calling `join()` on a CompletableFuture?
- AIt cancels the future's computation.
- BIt executes the future's task immediately.
- CIt blocks the current thread until the future is complete.
- DIt converts the future to a synchronous task.
8.Which CompletableFuture method allows you to run a task regardless of the outcome of the previous stage?
- AthenAcceptBoth
- Bhandle
- CwhenComplete
- DthenRun
9.What is the primary difference between `thenApply` and `thenApplyAsync` in CompletableFuture?
- A`thenApply` is used for exception handling.
- B`thenApplyAsync` runs the function in the same thread.
- C`thenApplyAsync` runs the function asynchronously.
- D`thenApply` runs the function asynchronously.
10.Which method would you use to ensure a CompletableFuture completes within a specific time frame?
- AorTimeout
- BcompleteExceptionally
- CapplyToEither
- DcompleteOnTimeout