nerdexam
Oracle

1Z0-809 · Question #191

Given the code fragment: List<String> l1 = Arrays.asList("Java", "J2EE", "J2ME", "JSTL", "JSP", "Oracle DB"); Predicate<String> pred = s -> s.contains("J2"); List<String> netL = l1.stream().filter(x…

The correct answer is D. [Java, J2EE, J2ME, JSTL]. There's an error in this question's answer key that's worth flagging directly. What the code actually does: 1. First filter (x.length() > 3) removes "JSP" (length 3) and keeps: [Java, J2EE, J2ME, JSTL, Oracle DB] 2. Second filter (pred = contains "J2") keeps only strings…

Question

Given the code fragment: List<String> l1 = Arrays.asList("Java", "J2EE", "J2ME", "JSTL", "JSP", "Oracle DB"); Predicate<String> pred = s -> s.contains("J2"); List<String> netL = l1.stream().filter(x -> x.length() > 3) .filter(pred).collect(Collectors.toList()); System.out.println(netL); What is the result?

Options

  • AA compilation error occurs.
  • B[Java, J2EE, J2ME, JSTL, JSP]
  • Cnull
  • D[Java, J2EE, J2ME, JSTL]

How the community answered

(38 responses)
  • A
    3% (1)
  • B
    13% (5)
  • C
    5% (2)
  • D
    79% (30)

Explanation

There's an error in this question's answer key that's worth flagging directly.

What the code actually does:

  1. First filter (x.length() > 3) removes "JSP" (length 3) and keeps: [Java, J2EE, J2ME, JSTL, Oracle DB]
  2. Second filter (pred = contains "J2") keeps only strings containing "J2": [J2EE, J2ME]

The actual output is [J2EE, J2ME] - which is not listed among the choices.

Why D is wrong (despite being marked correct): [Java, J2EE, J2ME, JSTL] is the result of applying only the first filter. The question's answer key appears to have ignored the second .filter(pred) entirely - a mistake in the exam itself.

Why the other distractors are wrong:

  • A - No compilation error; Predicate<String> works fine as a method reference to .filter()
  • B - Includes "JSP" (length 3), which fails the > 3 length check
  • C - collect(Collectors.toList()) never returns null; it returns an empty list at worst

Memory tip: When chaining .filter() calls, apply them mentally in sequence - each one narrows the stream further. The final result must satisfy all filter predicates simultaneously.

If this is from a real exam, the intended answer appears to be a question-writing error. The correct output for the given code is [J2EE, J2ME].

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice