1Z0-809 practice question 25 of 25
Given the code fragment: List list1 = Arrays.asList(10, 20); List list2 = Arrays.asList(15, 30); //line n1 Which code fragment, when inserted at line n1, prints 10 20 15 30?
Full set: 196 questionsThe goal is to create a single stream of integers from two separate lists and then print them. Stream.of(list1, list2) creates a Stream<List>. The flatMap operation is used to transform this stream of lists into a single, "flattened" Stream. The lambda expression list -> list.stream() provides the logic for this transformation, turning each list into a stream of its elements. Finally, forEach iterates through the flattened stream and prints each integer.
B. The List interface does not have an intStream() method. This code would result in a compilation error.
C. This code is syntactically incorrect. The argument to the first flatMap is an expression ending in forEach, which returns void, not a Stream.
D. The flatMapToInt method requires a function that returns an IntStream. The lambda list -> list.stream() returns a Stream, causing a type mismatch and a compilation error.
1. Oracle, "Java SE 8 API Documentation, java.util.stream.Stream interface, flatMap(Function<? super T, ? extends Stream> mapper) method". The documentation describes flatMap as returning "a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element."
2. Oracle, "The Java Tutorials, Aggregate Operations, The flatMap Operation". This tutorial provides examples of using flatMap to flatten complex structures into a single stream.
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
