1Z0-809 · Question #103
Given the code fragment: ``java List<String> empDetails = Arrays.asList("^100, Robin, HR", "^200, Mary, AdminServices", "^101, Peter, HR"); empDetails.stream() .filter(s-> s.contains("^")) .sorted()…
The correct answer is C. 101, Peter, HR 200, Mary, AdminServices. Option C is correct because the filter operation retains only strings containing "^" - eliminating "100, Robin, HR" (no caret) and keeping "^200, Mary, AdminServices" and "^101, Peter, HR". The subsequent sorted() applies natural lexicographic ordering, placing "^101..." before…
Question
List<String> empDetails = Arrays.asList("^100, Robin, HR", "^200, Mary, AdminServices", "^101, Peter, HR");
empDetails.stream()
.filter(s-> s.contains("^"))
.sorted()
.forEach(System.out::println); //line n1
What is the result?Options
- A100, Robin, HR 101, Peter, HR
- BA compilation error occurs at line n1.
- C101, Peter, HR 200, Mary, AdminServices
- D100, Robin, HR 200, Mary, AdminServices 101, Peter, HR
How the community answered
(44 responses)- A9% (4)
- B5% (2)
- C84% (37)
- D2% (1)
Explanation
Option C is correct because the filter operation retains only strings containing "^" - eliminating "100, Robin, HR" (no caret) and keeping "^200, Mary, AdminServices" and "^101, Peter, HR". The subsequent sorted() applies natural lexicographic ordering, placing "^101..." before "^200..." (since '1' < '2'), producing the two-line output matching C.
Why the distractors are wrong:
- A is wrong because it includes
"100, Robin, HR", which is filtered out (no^), and omits"200, Mary, AdminServices". - B is wrong because the code compiles and runs without error -
System.out::printlnis a valid method reference forConsumer<String>. - D is wrong because it shows all three strings in their original, unsorted insertion order, ignoring both the filter and the sort.
Memory tip: Think "FSF" - Filter first, then Sort, then ForEach. Always trace what survives the filter before worrying about ordering; exam traps often sneak in elements that look like they belong but never pass the predicate.
Community Discussion
No community discussion yet for this question.