Java OCA OCP Practice Question 3101

Question

Choose the correct option for this code segment:

List<String> lines = Arrays.asList("foo;bar;baz", "", "qux;Web");
lines.stream()
       .flatMap(line -> Arrays.stream(line.split(";"))) // FLAT
       .forEach(str -> System.out.print(str + ":"));
a)   this code will result in a compiler error in line marked with the comment FLAT
b)   this code will throw a java.lang.NullPointerException
c)   this code will throw a java.util.regex.PatternSyntaxException
d)   this code will print foo:bar:baz::qux:Web:


d)

Note

the flatMap() method flattens the streams by taking the elements in the stream.

the elements in the given strings are split using the separator ";" and the elements from the resulting string stream are collected.

the forEach() method prints the resulting strings.

option a) this code does not issue any compiler errors.

option b) this Splitting an empty string does not result in a null, and hence this code does not throw NullPointerException.

option c) the syntax of the given regular expression is correct and hence it does not result in PatternSyntaxException.




PreviousNext

Related