nerdexam
Oracle

1Z0-809 · Question #221

Given the definition of the Emp class: public class Emp { private String eName; private Integer eAge; Emp(String eN, Integer eA) { this.eName = eN; this.eAge = eA; } public Integer getEAge() {return…

The correct answer is A. Sam John Jim. Option A is correct (note: "Jim" in the answer appears to be a typo for "Jill") because the predicate s.getEAge() <= 60 evaluates to true for all three employees - Sam (20), John (60), and Jill (51) - so none are filtered out, and all three names are printed in insertion order…

Question

Given the definition of the Emp class: public class Emp { private String eName; private Integer eAge; Emp(String eN, Integer eA) { this.eName = eN; this.eAge = eA; } public Integer getEAge() {return eAge;} public String getEName() {return eName;} } and code fragment: List<Emp>ll = Arrays.asList(new Emp("Sam", 20), New Emp("John", 60), New Emp("Jill", 51)); Predicate<Emp> pEval = s -> s.getEAge() <= 60; //line n1 ll = ll.stream().filter(pEval).collect (Collectors.toList()); Stream<String> names = ll.stream().map (Emp::getEName); //line n2 names.forEach(n -> System.out.print (n + " ")); What is the result?

Options

  • ASam John Jim
  • BJohn Jim
  • CA compilation error occurs at line n1.
  • DA compilation error occurs at line n2.

How the community answered

(36 responses)
  • A
    83% (30)
  • B
    6% (2)
  • C
    8% (3)
  • D
    3% (1)

Explanation

Option A is correct (note: "Jim" in the answer appears to be a typo for "Jill") because the predicate s.getEAge() <= 60 evaluates to true for all three employees - Sam (20), John (60), and Jill (51) - so none are filtered out, and all three names are printed in insertion order.

Option B is wrong because it excludes Sam, which would only make sense if the predicate were < 60 or > some_threshold; since 20 ≤ 60 is true, Sam passes the filter and appears in the output.

Option C is wrong because the lambda s -> s.getEAge() <= 60 is perfectly valid for Predicate<Emp> - it takes an Emp argument and returns a boolean, satisfying the functional interface contract.

Option D is wrong because Emp::getEName is a valid method reference that maps Emp → String, making it a legal argument to stream().map() which produces a Stream<String>.

Memory tip: When evaluating filter predicates on exams, quickly substitute each element's value and check the condition - don't assume the predicate eliminates anything without testing the boundary values (here 60 <= 60 is true, which catches test-takers who think of <= as <).

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice