Java Streams API (map, filter, reduce) Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is the primary purpose of the Java Streams API?

A
To provide functional-style operations on collections
B
To replace all loops in Java code
C
To create new collection types
D
To handle file I/O operations
2

Question 2

How do you create a stream from a List?

java
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();
A
Using the stream() method on the List
B
Using Stream.of(list)
C
Using new Stream<>(list)
D
Using Arrays.stream(list)
3

Question 3

What does the map() intermediate operation do?

A
Transforms each element using a function
B
Filters elements based on a predicate
C
Combines all elements into one result
D
Sorts the elements
4

Question 4

How do you create a stream from an array?

java
String[] array = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(array);
A
Using Arrays.stream(array)
B
Using array.stream()
C
Using Stream.of(array)
D
Using new Stream<>(array)
5

Question 5

What is the difference between intermediate and terminal operations?

A
Intermediate operations return a stream, terminal operations return a result or void
B
Intermediate operations are lazy, terminal operations are eager
C
Both A and B are correct
D
Intermediate operations modify the source, terminal operations don't
6

Question 6

What does the filter() operation do?

java
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> even = numbers.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());
A
Selects elements that match a predicate
B
Transforms elements using a function
C
Sorts elements in ascending order
D
Removes duplicate elements
7

Question 7

How do you create an infinite stream?

java
Stream<Integer> infinite = Stream.iterate(1, n -> n + 1);
A
Using Stream.iterate() or Stream.generate()
B
Using Stream.of() with no arguments
C
Using an empty collection.stream()
D
Infinite streams cannot be created
8

Question 8

What is a reduction operation in streams?

A
An operation that combines stream elements into a single result
B
An operation that filters elements
C
An operation that transforms elements
D
An operation that sorts elements
9

Question 9

How do you collect stream results into a List?

java
List<String> result = stream.collect(Collectors.toList());
A
Using collect(Collectors.toList())
B
Using toList() directly
C
Using collect(Collectors.toCollection(ArrayList::new))
D
All of the above work
10

Question 10

What does the distinct() intermediate operation do?

java
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3);
List<Integer> unique = numbers.stream()
    .distinct()
    .collect(Collectors.toList());
A
Removes duplicate elements
B
Sorts elements in ascending order
C
Limits the number of elements
D
Reverses the order of elements
11

Question 11

How do you find the first element of a stream?

java
Optional<String> first = stream.findFirst();
A
Using findFirst() which returns Optional
B
Using first() directly
C
Using get(0) on the stream
D
Using findAny() and hoping for the first
12

Question 12

What is the purpose of the limit() operation?

java
Stream<Integer> limited = Stream.iterate(1, n -> n + 1)
    .limit(5);
A
Limits the stream to a maximum number of elements
B
Limits processing time
C
Limits memory usage
D
Limits the number of operations
13

Question 13

How do you perform a reduction with reduce()?

java
int sum = numbers.stream()
    .reduce(0, (a, b) -> a + b);
A
Using reduce(identity, accumulator)
B
Using reduce(accumulator) only
C
Using collect() with summing
D
Using sum() directly
14

Question 14

What does the flatMap() operation do?

java
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());
A
Flattens nested collections into a single stream
B
Creates a map from stream elements
C
Applies a function and flattens the result
D
Both A and C are correct
15

Question 15

How do you check if any element matches a predicate?

java
boolean anyEven = numbers.stream()
    .anyMatch(n -> n % 2 == 0);
A
Using anyMatch(predicate)
B
Using contains() on the stream
C
Using findAny() and checking
D
Using someMatch(predicate)
16

Question 16

What is the difference between findFirst() and findAny()?

A
findFirst() is deterministic, findAny() may return any element for parallel streams
B
findFirst() is for ordered streams, findAny() for unordered
C
findFirst() returns the first element, findAny() returns a random element
D
They are identical in behavior
17

Question 17

How do you sort a stream?

java
List<String> sorted = strings.stream()
    .sorted()
    .collect(Collectors.toList());
A
Using sorted() for natural order or sorted(comparator) for custom order
B
Using sort() method
C
Using orderBy()
D
Using arrange()
18

Question 18

What does the peek() operation do?

java
stream.peek(System.out::println)
    .collect(Collectors.toList());
A
Performs an action on each element without modifying the stream
B
Looks at the next element without consuming it
C
Returns the first element
D
Skips elements in the stream
19

Question 19

How do you count elements in a stream?

java
long count = stream.count();
A
Using the count() terminal operation
B
Using size() method
C
Using length() method
D
Using Collectors.counting()
20

Question 20

What is a stream pipeline?

A
A sequence of stream operations chained together
B
A physical pipe connecting stream sources
C
A storage mechanism for stream data
D
A type of stream collector
21

Question 21

How do you create a parallel stream?

java
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> parallelStream = list.parallelStream();
A
Using parallelStream() on collections
B
Using stream().parallel()
C
Both A and B
D
Using ParallelStream.of()
22

Question 22

What is the difference between sequential and parallel streams?

A
Sequential processes elements one by one, parallel may process concurrently
B
Sequential is faster, parallel is slower
C
Sequential uses more memory, parallel uses less
D
Sequential is deprecated, parallel is recommended
23

Question 23

How do you group elements by a classifier?

java
Map<String, List<Person>> byCity = people.stream()
    .collect(Collectors.groupingBy(Person::getCity));
A
Using Collectors.groupingBy(classifier)
B
Using groupBy() on streams
C
Using partitionBy() with a predicate
D
Using categorize() method
24

Question 24

What does the skip() operation do?

java
Stream<Integer> skipped = numbers.stream().skip(3);
A
Skips the first n elements of the stream
B
Skips elements that match a predicate
C
Skips null elements
D
Skips duplicate elements
25

Question 25

How do you concatenate two streams?

java
Stream<String> combined = Stream.concat(stream1, stream2);
A
Using Stream.concat(stream1, stream2)
B
Using stream1.addAll(stream2)
C
Using stream1.merge(stream2)
D
Using flatMap with both streams
26

Question 26

What is stream laziness?

A
Intermediate operations are not executed until a terminal operation is called
B
Streams delay processing to save memory
C
Streams are lazy by default and eager when needed
D
All of the above are aspects of laziness
27

Question 27

How do you handle checked exceptions in streams?

A
Wrap in try-catch or use functional interfaces that throw unchecked exceptions
B
Streams automatically handle checked exceptions
C
Use try-with-resources in stream operations
D
Checked exceptions cannot be used in streams
28

Question 28

What is the purpose of the forEach() terminal operation?

java
numbers.stream().forEach(System.out::println);
A
Performs an action for each element in the stream
B
Iterates over elements without collecting results
C
Both A and B
D
Replaces traditional for-each loops
29

Question 29

How do you create a stream from a range of numbers?

java
IntStream range = IntStream.range(1, 10); // 1 to 9
IntStream rangeClosed = IntStream.rangeClosed(1, 10); // 1 to 10
A
Using IntStream.range(start, end) or rangeClosed(start, end)
B
Using Stream.range(start, end)
C
Using Arrays.stream() with an array
D
Using List.of() and stream()
30

Question 30

What is a common pitfall when using parallel streams?

A
Using non-thread-safe operations or shared mutable state
B
Parallel streams are always slower
C
Parallel streams don't work with infinite streams
D
Parallel streams cannot be collected
31

Question 31

How do you partition a stream into two groups?

java
Map<Boolean, List<Person>> partitioned = people.stream()
    .collect(Collectors.partitioningBy(person -> person.getAge() >= 18));
A
Using Collectors.partitioningBy(predicate)
B
Using Collectors.groupingBy() with boolean classifier
C
Using filter() twice
D
Using splitBy() method
32

Question 32

What does the allMatch() operation check?

java
boolean allEven = numbers.stream()
    .allMatch(n -> n % 2 == 0);
A
Whether all elements match the predicate
B
Whether any element matches the predicate
C
Whether no elements match the predicate
D
Whether exactly one element matches
33

Question 33

How do you create a stream from a file?

java
try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) {
    lines.forEach(System.out::println);
}
A
Using Files.lines(path) which returns a stream of lines
B
Using new FileInputStream() and stream()
C
Using Scanner and stream()
D
Using BufferedReader.lines()
34

Question 34

What is the benefit of using streams over traditional loops?

A
More readable, composable, and potentially parallelizable code
B
Faster execution in all cases
C
Less memory usage
D
Automatic error handling
35

Question 35

Mastering Java Streams API means you now write code that:

A
Processes collections functionally with map, filter, reduce operations, creates streams from various sources, applies intermediate and terminal operations correctly, and follows best practices for efficient and readable stream processing
B
Always uses parallel streams for better performance
C
Avoids all traditional loops entirely
D
Uses streams only for simple filtering operations

QUIZZES IN Java