1Z0-809 · Question #83
Given the code fragments: interface CourseFilter extends Predicate<String> { public default boolean test (String str) { return str.equals ("Java"); } } and List<String> strs = Arrays.asList("Java"…
The correct answer is B. 3. Option B is correct because all three strings survive both filters. cf1 tests length() > 3 - "Java" has 4 characters (4 > 3 is true), so all three strings pass. cf2 overrides test() to check s.contains("Java"), which is also true for all three strings, giving a final count of…
Question
Options
- A2
- B3
- CA compilation error occurs at line n1.
- DA compilation error occurs at line n2.
How the community answered
(55 responses)- A4% (2)
- B73% (40)
- C7% (4)
- D16% (9)
Explanation
Option B is correct because all three strings survive both filters. cf1 tests length() > 3 - "Java" has 4 characters (4 > 3 is true), so all three strings pass. cf2 overrides test() to check s.contains("Java"), which is also true for all three strings, giving a final count of 3.
A (2) is wrong because test-takers often assume "Java" fails cf1 - perhaps confusing > 3 with > 4 or >= 4. Since 4 > 3 evaluates to true, "Java" is not filtered out, so all three strings reach cf2.
C is wrong because line n1 is perfectly legal Java: you can instantiate an interface using an anonymous class syntax, and since CourseFilter is a functional interface extending Predicate<String>, the assignment to Predicate<String> cf2 is valid. D is wrong because cf2 is declared as Predicate<String>, which is exactly what Stream.filter() expects - no type mismatch exists at line n2.
Memory tip: When tracing stream pipelines, write each string in a column and tick or cross it at each filter stage - a length of 4 clears the "> 3" bar, so "Java" survives. Never assume a string is filtered without explicitly evaluating the predicate.
Community Discussion
No community discussion yet for this question.