Java Streams MCQs with answers
58 questions tagged Java Streams, each with the correct answer and an explanation. Page 1 of 3.
- What happens if duplicate keys are provided to `Collectors.toMap()`?
- What does `Collectors.toCollection(TreeSet::new)` do?
- Which stream operation is best suited to calculate the average of a list of numbers?
- What is the output type of `Collectors.toMap()`?
- Which method will throw if there is no element in the stream?
- What is the result of `Stream.of(1, 2, 3).reduce(1, (a, b) -> a * b)`?
- How can you avoid a `NullPointerException` while using streams?
- What is the default behavior of `sorted()`?
- Which collector is used to partition a stream into two groups?
- What is the difference between `map()` and `flatMap()`?
- What does `flatMapToInt()` return?
- Which of the following types is a primitive specialization of Stream?
- Which method is used to create a Stream of primitive ints?
- Which method returns true if the stream contains no elements?
- Which of the following is not a valid intermediate operation?
- What is returned by `reduce()` when no identity is provided and the stream is empty?
- Which method would you use to get the maximum value from a Stream?
- What happens if `stream()` is called on a null List?
- What does the `findAny()` method return?
- Which method returns the default value without inserting a new key?