LearnFrenzy
Sign in Take a free test

Lambda Expressions · Find the Error · Question 4

Error: a lambda parameter with the name of a local variable Answer, explanation & discussion

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

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

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links