Java Streams API (map, filter, reduce) Quiz
Master the Java Streams API for functional-style operations on collections. Learn to create streams, use intermediate operations like map and filter, apply terminal operations, perform reductions, and follow best practices for efficient stream processing.
Question 1
What is the primary purpose of the Java Streams API?
Question 2
How do you create a stream from a List?
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();Question 3
What does the map() intermediate operation do?
Question 4
How do you create a stream from an array?
String[] array = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(array);Question 5
What is the difference between intermediate and terminal operations?
Question 6
What does the filter() operation do?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> even = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());Question 7
How do you create an infinite stream?
Stream<Integer> infinite = Stream.iterate(1, n -> n + 1);Question 8
What is a reduction operation in streams?
Question 9
How do you collect stream results into a List?
List<String> result = stream.collect(Collectors.toList());Question 10
What does the distinct() intermediate operation do?
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3);
List<Integer> unique = numbers.stream()
.distinct()
.collect(Collectors.toList());Question 11
How do you find the first element of a stream?
Optional<String> first = stream.findFirst();Question 12
What is the purpose of the limit() operation?
Stream<Integer> limited = Stream.iterate(1, n -> n + 1)
.limit(5);Question 13
How do you perform a reduction with reduce()?
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);Question 14
What does the flatMap() operation do?
List<List<String>> listOfLists = Arrays.asList(
Arrays.asList("a", "b"),
Arrays.asList("c", "d")
);
List<String> flat = listOfLists.stream()
.flatMap(List::stream)
.collect(Collectors.toList());Question 15
How do you check if any element matches a predicate?
boolean anyEven = numbers.stream()
.anyMatch(n -> n % 2 == 0);Question 16
What is the difference between findFirst() and findAny()?
Question 17
How do you sort a stream?
List<String> sorted = strings.stream()
.sorted()
.collect(Collectors.toList());Question 18
What does the peek() operation do?
stream.peek(System.out::println)
.collect(Collectors.toList());Question 19
How do you count elements in a stream?
long count = stream.count();Question 20
What is a stream pipeline?
Question 21
How do you create a parallel stream?
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> parallelStream = list.parallelStream();Question 22
What is the difference between sequential and parallel streams?
Question 23
How do you group elements by a classifier?
Map<String, List<Person>> byCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));Question 24
What does the skip() operation do?
Stream<Integer> skipped = numbers.stream().skip(3);Question 25
How do you concatenate two streams?
Stream<String> combined = Stream.concat(stream1, stream2);Question 26
What is stream laziness?
Question 27
How do you handle checked exceptions in streams?
Question 28
What is the purpose of the forEach() terminal operation?
numbers.stream().forEach(System.out::println);Question 29
How do you create a stream from a range of numbers?
IntStream range = IntStream.range(1, 10); // 1 to 9
IntStream rangeClosed = IntStream.rangeClosed(1, 10); // 1 to 10Question 30
What is a common pitfall when using parallel streams?
Question 31
How do you partition a stream into two groups?
Map<Boolean, List<Person>> partitioned = people.stream()
.collect(Collectors.partitioningBy(person -> person.getAge() >= 18));Question 32
What does the allMatch() operation check?
boolean allEven = numbers.stream()
.allMatch(n -> n % 2 == 0);Question 33
How do you create a stream from a file?
try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) {
lines.forEach(System.out::println);
}Question 34
What is the benefit of using streams over traditional loops?
Question 35
Mastering Java Streams API means you now write code that:
