Question 2
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!