Java SE 8 Programmer II — Question 82

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: C

Explanation

The code filters the colors that have a length of 3 or more and checks if all of them contain 'red'. Since 'red', 'green', and 'yellow' pass the filter, the test predicate is executed three times, resulting in 'Searching' being printed three times. Options A and B are incorrect as they do not reflect the total prints, and option D is wrong because the code compiles successfully.