🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Streams in Depth: all 10 questions

Quiz: Java Streams in Depth · Question 4 of 10

What does this print? (code)

What does this print?

import java.util.*;
import java.util.stream.*;
public class Main {
    public static void main(String[] args) {
        List<List<Integer>> a = List.of(List.of(1, 2), List.of(3), List.of());
        System.out.println(a.stream().flatMap(List::stream).mapToInt(Integer::intValue).sum());
    }
}
  1. A.3
  2. B.6
  3. C.[1, 2, 3]
  4. D.Compile error
Answer & explanation

Correct answer: 6

flatMap joins the three lists into one stream 1, 2, 3; their sum is 6. The empty list adds nothing.

Can you get the rest right without peeking?

Take the timed Quiz: Java Streams in Depth quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →