1Z0-809 practice question 6 of 25

Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Mary, AdminServices”,“101, Peter…

Full set: 196 questions

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
A:

A. 100, Robin, HR101, Peter, HR

Correct answer
B:

B. A compilation error occurs at line n1.

C:

C. 100, Robin, HR101, Peter, HR200, Mary, AdminServices

D:

D. 100, Robin, HR200, Mary, AdminServices101, Peter, HR

Correct Answer:
A. A. 100, Robin, HR101, Peter, HR
Explanation

The stream pipeline first filters the empDetails list. The filter(s-> s.contains("1")) operation retains elements that contain the character "1", which are "100, Robin, HR" and "101, Peter, HR". The sorted() operation then sorts these two remaining strings in their natural (lexicographical) order, which they are already in. Finally, forEach(System.out::println) iterates through the sorted stream and prints each element on a new line. The resulting sequence of printed strings is "100, Robin, HR" followed by "101, Peter, HR".

Why Incorrect

B. The code is syntactically valid. Stream operations are correctly chained, and the method reference is appropriate for forEach.
C. The string "200, Mary, AdminServices" is incorrectly included; it should be removed by the filter operation as it does not contain "1".
D. The order is incorrect. The sorted() method guarantees a natural lexicographical sort, placing "100..." before "101...".

References

- Oracle, Java SE 8 Documentation, java.util.stream.Stream. The documentation for filter(Predicate predicate) and sorted() describes their behavior.

- Oracle, The Java™ Tutorials, "Lesson: Aggregate Operations". This tutorial explains the behavior of intermediate operations like filter and sorted.

Scroll to Top