Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
int x = 5;
IntUnaryOperator next = x -> x + 1;
System.out.println(next.applyAsInt(x));
}
}
- A IntUnaryOperator cannot add
- B A lambda does not create a new scope, so its parameter cannot reuse the name of the local variable x Correct
- C x must be final
- D applyAsInt needs a long
Correct Answer: Option B
javac: "variable x is already defined in method main". An anonymous class would allow shadowing.
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!