Java SE 8 Programmer II — Question 7

Given the code fragment:
List<Integer> list1 = Arrays.asList(10, 20);
List<Integer> list2 = Arrays.asList(15, 30);
//line n1
Which code fragment, when inserted at line n1, prints 10 20 15 30?

Answer options

Correct answer: A

Explanation

The correct answer, A, uses flatMap to merge the two lists into a single stream, allowing for all elements to be printed in order. Option B is incorrect because intStream() does not apply to List<Integer>. Option C has a syntax error and incorrect stream handling, while option D incorrectly uses flatMapToInt(), which is not suitable for List<Integer> and does not produce the desired output.