Java SE 8 Programmer II — Question 73
Given the code fragment:
List<String> empDetails = Arrays.asList("100, Robin, HR", "200, Mary, AdminServices","101, Peter, HR"); empDetails.stream()
.filter(s-> s.contains("r"))
.sorted()
.forEach(System.out::println); //line n1
What is the result?
Answer options
- A. 100, Robin, HR 101, Peter, HR
- B. E. A compilation error occurs at line n1.
- C. 101, Peter, HR 200, Mary, AdminServices
- D. 100, Robin, HR 200, Mary, AdminServices 101, Peter, HR
Correct answer: D
Explanation
The code filters for strings containing 'r', which includes all the entries in the list. After filtering, the entries are sorted alphabetically, resulting in '100, Robin, HR', '200, Mary, AdminServices', and '101, Peter, HR'. The correct output is option D, as it lists all entries in the expected sorted order. Options A, B, and C do not represent the correct filtered and sorted output.