Java OCA OCP Practice Question 3119

Question

Choose the correct option based on this code segment:

Stream<String> words = Stream.of("Web", "mWeb", "SQL", "MySQL");
// LINE_ONE
String boxedString = words.collect(Collectors.joining(", ", "[", "]"));
// LINE_TWO
System.out.println(boxedString);
a)this code results in a compiler error in line marked with the comment LINE_ONE
b)this code results in a compiler error in line marked with the comment LINE_TWO
c)this program prints: [Web, mWeb, SQL, MySQL]
d)this program prints: [Web], [mWeb], [SQL], [MySQL]


c)

Note

Stream.of() method takes a variable length argument list of type T and it returns a Stream<T>.

the joining() method in Collectors class takes delimiter, prefix, and suffix as arguments:

joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

hence, the expression Collectors.joining(", ", "[", "]") joins the strings with commas and encloses the resulting string within '[' and ']'.




PreviousNext

Related