Java SE 8 Programmer II — Question 42

Given the code fragment:
List<String> colors = Arrays.asList("red", "green", "yellow");
Predicate<String> test = n - > {
System.out.println("Searching"¦");
return n.contains("red");
};
colors.stream()
.filter(c -> c.length() > 3)
.allMatch(test);
What is the result?

Answer options

Correct answer: A

Explanation

The correct answer is A because the filter only allows strings with a length greater than 3, which in this case only includes 'green' and 'yellow'. The predicate only outputs 'Searching...' when it checks 'green', which does not contain 'red', thus only printing once before the allMatch returns false. The other options are incorrect as they imply more outputs or a compilation error that does not occur.