nerdexam
Oracle

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

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", "Java EE", "Java ME"); Predicate<String> cf1 = s -> s.length() > 3; Predicate<String> cf2 = new CourseFilter() { //line n1 public boolean test (String s) { return s.contains ("Java"); } }; long c = strs.stream() .filter (cf1) .filter (cf2)//line n2 .count(); System.out.println (c); What is the result?

Options

  • A2
  • B3
  • CA compilation error occurs at line n1.
  • DA compilation error occurs at line n2.

How the community answered

(55 responses)
  • A
    4% (2)
  • B
    73% (40)
  • C
    7% (4)
  • D
    16% (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.

Full 1Z0-809 Practice