1Z0-809 practice question 13 of 25

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

Full set: 196 questions
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: Listli = Arrays.asList(new Emp(“Sam”, 20), New Emp(“John”, 60), New Emp(“Jim”, 51)); Predicate agVal = s -> s.getEAge() > 50;//line n1 li = li.stream().filter(agVal).collect(Collectors.toList()); Stream names = li.stream()map.(Emp::getEName);//line n2 names.forEach(n -> System.out.print(n + “ “)); What is the result?
Answer options
A: Sam John Jim
B: John Jim Correct answer
C: A compilation error occurs at line n1.
D: A compilation error occurs at line n2.
Correct Answer:
B. John Jim
Explanation

The code first filters a list of Emp objects, keeping only those whose age is greater than 50. The initial list contains employees with ages 20, 60, and 51. The filter operation s -> s.getEAge() > 50 retains the employees "John" (age 60) and "Jim" (age 51). The subsequent map operation extracts the names of these filtered employees into a new stream. Finally, forEach prints each name from the stream, separated by a space. The resulting output is "John Jim ". Note: The code in the question contains typos (New for new, map.(... for map(...)), which are assumed to be unintentional for the purpose of evaluating the stream logic.

Why Incorrect

A: This is the list of all employee names before the filter is applied. The filter agVal removes "Sam" from the stream. C: Line n1 is a syntactically correct definition of a Predicate using a lambda expression. It does not cause a compilation error. D: Assuming the typo map.(... is corrected to map(...), this line is a valid stream operation and does not cause a compilation error. The original code as written would fail here.

References

- Java™ Platform, Standard Edition 8 API Specification, java.util.stream.Stream. The documentation describes the filter(Predicate predicate) and map(Function mapper) intermediate operations.

- The Java™ Tutorials, "Aggregate Operations". Oracle Corporation. This tutorial provides examples of using filter, map, and forEach to process collections.

Scroll to Top