LearnFrenzy
Sign in Take a free test

Lambda Expressions · Find the Error · Question 10

Error: a Predicate lambda returning an int Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

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

Main.java
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Predicate<String> longWord = s -> s.length();
        System.out.println(longWord.test("generics"));
    }
}
  • A test is not a method
  • B Predicate.test must return boolean, but s.length() is an int; write s -> s.length() > 5 Correct
  • C Predicate needs two type arguments
  • D s must be declared
Correct Answer: Option B

javac: "bad return type in lambda expression: int cannot be converted to boolean".

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