Java SE 8 Programmer II — Question 87
Given the code fragments:
interface CourseFilter extends Predicate<String> {
public default boolean test (String str) {
return str.contains ("Java");
}
}
and
List<String> strs = Arrays.asList("Java", "Java EE", "Embedded Java");
Predicate<String> cf1 = s - > s.length() > 3;
Predicate cf2 = new CourseFilter() { //line n1
public boolean test (String s) {
return s.startsWith ("Java");
}
};
long c = strs.stream()
.filter(cf1)
.filter(cf2 //line n2
.count();
System.out.println(c);
What is the result?
Answer options
- A. 2
- B. 3
- C. A compilation error occurs at line n1.
- D. A compilation error occurs at line n2.
Correct answer: D
Explanation
The correct answer is D because the filter method at line n2 is attempting to use a Predicate with a method that doesn't match the expected signature, causing a compilation error. Option C is incorrect as line n1 does not generate a compilation issue; the problem arises from line n2.