LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Find the Error · Question 8

Error: a thenApply result of the wrong type Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        CompletableFuture<Integer> length = CompletableFuture.supplyAsync(() -> "learnfrenzy").thenApply(String::toUpperCase);
        System.out.println(length.join());
    }
}
  • A toUpperCase cannot be a method reference
  • B thenApply(String::toUpperCase) produces a CompletableFuture<String>, not <Integer>; use thenApply(String::length) or change the type Correct
  • C supplyAsync cannot return a String
  • D join returns void
Correct Answer: Option B

The function given to thenApply decides the new future's type.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links