ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
String r = Stream.of(3, 1, 3, 2).distinct().sorted()
                 .map(String::valueOf).collect(Collectors.joining("-"));
System.out.print(r);
  1. A. 3-1-2
  2. B. 1-2-3-3
  3. C. 3-2-1
  4. D. 1-2-3

Correct answer: D. 1-2-3

distinct keeps the first occurrence of each value (3, 1, 2), sorted orders them naturally, and joining concatenates with the separator.

More OOP with C++ and Java questions

Exams that ask this topic