LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Find the Error · Question 2

Error: a Callable lambda that returns nothing Answer, explanation & discussion

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

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

Main.java
import java.util.concurrent.Callable;

public class Main {
    public static void main(String[] args) throws Exception {
        Callable<Integer> task = () -> {
            int total = 0;
            for (int i = 1; i <= 10; i++) total += i;
        };
        System.out.println(task.call());
    }
}
  • A call() cannot be invoked directly
  • B Callable<Integer>.call must return an Integer, but the block body has no return statement; add return total; Correct
  • C total must be Integer
  • D Lambdas cannot contain loops
Correct Answer: Option B

javac reports a missing return value in the lambda.

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