Java SE 8 Programmer II — Question 25

Given the code fragment:
List<String> empDetails = Arrays.asList("100, Robin, HR",
"200, Mary, AdminServices",
"101, Peter, HR");
empDetails.stream()
.filter(s-> s.contains("1"))
.sorted()
.forEach(System.out::println); //line n1
What is the result?

Answer options

Correct answer: C

Explanation

The correct answer is C because the stream filters the list for strings containing '1', which includes both '100, Robin, HR' and '101, Peter, HR'. After filtering, the results are sorted, and all matching entries are printed. Options A and D do not include all filtered results, while B incorrectly states that a compilation error occurs.