Java Streams API Quiz - MCQ - Multiple Choice Questions

This post contains a set of 25 multiple-choice questions covering various aspects of the Java Streams API, including its operations, methods, and general characteristics. Go ahead and take this quiz to test your knowledge of Java Streams API.

1. What is the correct term for the operations that produce a result from a stream but do not modify its source?

a) Mutable operations
b) Intermediate operations
c) Terminal operations
d) Source operations

Answer:

c) Terminal operations

Explanation:

Terminal operations, such as forEach, collect, or reduce, produce a result from a stream but do not modify its source. They are typically used to produce side effects or to obtain a final result from the stream.

2. Which method is used to transform each element of a Stream using a provided function?

a) filter
b) map
c) flatMap
d) reduce

Answer:

b) map

Explanation:

The map method is used to transform each element of a stream using a provided function. It applies a function to each element and maps the elements to new values.

3. What does the Stream.peek method do?

a) Consumes and removes an element from the stream
b) Performs an action on each element of the stream without modifying it
c) Filters elements based on a predicate
d) Reduces the stream to a single summary element

Answer:

b) Performs an action on each element of the stream without modifying it

Explanation:

The peek method is mainly used for debugging purposes. It allows you to perform an action on each element of the stream as it is consumed, without modifying the elements themselves.

4. Which of these collectors is used for grouping elements of a Stream?

a) Collectors.toList()
b) Collectors.toSet()
c) Collectors.groupingBy()
d) Collectors.joining()

Answer:

c) Collectors.groupingBy()

Explanation:

Collectors.groupingBy() is used for grouping elements of the stream into a Map based on a classifier function.

5. What type of operation is Stream.filter?

a) Terminal
b) Intermediate
c) Mutable
d) Source

Answer:

b) Intermediate

Explanation:

Stream.filter is an intermediate operation. It returns a new stream that includes elements that match a given predicate.

6. What is the purpose of the Stream.flatMap method?

a) To merge multiple streams into one
b) To replace each element with a stream and then flatten the streams into a single stream
c) To map each element to a new value
d) To filter elements based on a flat structure

Answer:

b) To replace each element with a stream and then flatten the streams into a single stream

Explanation:

The flatMap method is used to replace each element of the stream with a stream of other objects and then flatten all the streams into a single stream.

7. Which of these options correctly describes a characteristic of Java Stream operations?

a) Streams operations modify the underlying data structure
b) Stream operations are always executed in parallel
c) Streams operations are lazy where possible
d) Stream operations cannot be chained

Answer:

c) Streams operations are lazy where possible

Explanation:

Stream operations are lazy where possible. Intermediate operations are not executed until a terminal operation is invoked.

8. What is the result of applying the Stream.reduce operation?

a) A new stream with elements reduced according to a provided accumulator function
b) A boolean indicating if any stream elements match a given predicate
c) An Optional describing the reduced value, if any
d) A list of elements that were reduced

Answer:

c) An Optional describing the reduced value, if any

Explanation:

Stream.reduce applies a binary operator to combine all elements of the stream. The result is an Optional describing the reduced value.

9. How does the forEach method in the Stream API differ from using a for-each loop?

a) forEach method is parallelizable, while the for-each loop is not
b) There is no difference
c) forEach can only be applied to collections, not streams
d) The for-each loop is more efficient than forEach

Answer:

a) forEach method is parallelizable, while the for-each loop is not

Explanation:

The forEach method of the Stream API can be parallelized automatically when used with a parallel stream, unlike the traditional for-each loop.

10. Which of these is a terminal operation in the Stream API?

a) map
b) flatMap
c) forEach
d) filter

Answer:

c) forEach

Explanation:

forEach is a terminal operation in the Stream API that is typically used to iterate over the stream elements and perform an action on each element.

11. What does the Stream.collect method do?

a) Filters elements from the stream
b) Reduces the stream to a single value
c) Performs an action on each element of the stream
d) Transforms the stream into a different form, such as a collection or a map

Answer:

d) Transforms the stream into a different form, such as a collection or a map

Explanation:

Stream.collect is a terminal operation that transforms the elements of the stream into a different form, such as a List, Set, or Map, using a provided Collector.

12. Which of the following correctly creates a stream from a collection?

a) Collection.stream()
b) Stream.of(collection)
c) collection.getStream()
d) Stream.from(collection)

Answer:

a) Collection.stream()

Explanation:

The stream() method is used to create a stream from a collection. It is a default method of the Collection interface.

13. What is the main advantage of using parallel streams in Java?

a) They simplify the syntax for creating streams
b) They make the code easier to read and maintain
c) They can improve performance by utilizing multiple cores of the processor
d) They prevent concurrent modification exceptions

Answer:

c) They can improve performance by utilizing multiple cores of the processor

Explanation:

Parallel streams in Java can improve performance by splitting the workload into multiple parts and utilizing multiple cores of the processor to process these parts in parallel.

14. What is the purpose of the Optional class in the context of Java streams?

a) To provide a container object which may or may not contain a value
b) To mark a stream as optional
c) To wrap stream operations that might not return a value
d) To provide an alternative stream if the original stream is empty

Answer:

a) To provide a container object which may or may not contain a value

Explanation:

Optional is often used in the context of streams to represent the result of operations that may not yield an actual value, like Stream.findFirst() or Stream.reduce().

15. Which of these methods is used to convert a stream to an array?

a) toArray()
b) toList()
c) collect()
d) toSet()

Answer:

a) toArray()

Explanation:

The toArray() method is used to convert a stream into an array. The method returns an array containing the elements of the stream.

16. What is the difference between Stream.findFirst() and Stream.findAny()?

a) findFirst() returns the first element, findAny() returns any element
b) findFirst() is for ordered streams, findAny() is for unordered streams
c) findAny() is faster on parallel streams, findFirst() is not
d) All of the above

Answer:

d) All of the above

Explanation:

findFirst() returns the first element of the stream, while findAny() can return any element. findAny() is typically faster on parallel streams, as it does not require the stream to be processed in order.

17. Which of the following is true about the intermediate operations in Java Streams?

a) They run immediately when called
b) They do not change the original data structure
c) They can only be applied to numeric streams
d) They produce a single value as output

Answer:

b) They do not change the original data structure

Explanation:

Intermediate operations in Java Streams, such as filter, map, and sorted, do not modify the original data structure. They instead return a new stream with the applied changes.

18. What is the main purpose of the Stream.sorted method?

a) To sort the elements of the stream in their natural order or by a provided comparator
b) To randomly shuffle the elements of the stream
c) To reverse the order of elements in the stream
d) To remove duplicate elements from the stream

Answer:

a) To sort the elements of the stream in their natural order or by a provided comparator

Explanation:

The sorted method is used to sort the elements of the stream. It can sort elements in their natural order or using a provided Comparator.

19. How can you create a stream from an array in Java?

a) Arrays.stream(array)
b) Stream.of(array)
c) Stream.fromArray(array)
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can create a stream from an array using Arrays.stream(array) or Stream.of(array). Both methods are correct and commonly used.

20. What does the IntStream.range function do?

a) Creates a stream of integers from 0 to the specified value
b) Creates a stream of integers within the specified range
c) Filters integers in a stream within the specified range
d) Converts a stream of objects to a stream of integers within the specified range

Answer:

b) Creates a stream of integers within the specified range

Explanation:

IntStream.range creates a stream of integers from the start value (inclusive) to the end value (exclusive) specified as parameters.

21. What is the use of the Stream.concat method?

a) To merge two streams into one
b) To add elements to the end of a stream
c) To combine the elements of a stream based on a predicate
d) To create a new stream from the concatenation of elements

Answer:

a) To merge two streams into one

Explanation:

Stream.concat is used to concatenate two streams into one. It creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

22. Which method is used in Java Streams to perform a reduction on the elements of a stream with the initial value?

a) reduce()
b) collect()
c) map()
d) forEach()

Answer:

a) reduce()

Explanation:

The reduce() method is used to perform a reduction on the elements of a stream, where an initial value is provided, and an accumulator function is applied to combine the current result with each element.

23. What is the return type of the Stream.mapToInt method?

a) Stream<Integer>
b) IntStream
c) List<Integer>
d) Integer[]

Answer:

b) IntStream

Explanation:

The mapToInt method converts a Stream<T> to an IntStream by applying a provided ToIntFunction<T> to each element, which helps in performing numeric operations on the stream elements.

24. What does the Stream.allMatch method do?

a) Checks if all elements of the stream match the given predicate
b) Returns true if any elements of the stream match the given predicate
c) Returns true if none of the elements of the stream match the given predicate
d) Applies a function to all elements of the stream

Answer:

a) Checks if all elements of the stream match the given predicate

Explanation:

The allMatch method checks whether all elements of the stream match the provided predicate. It returns true if all elements match the predicate; otherwise, it returns false.

25. Which of these is not a characteristic of Java Streams?

a) Streams can be traversed only once
b) Streams operations are inherently parallel
c) Streams do not store elements
d) Streams support behavioral parameterization

Answer:

b) Streams operations are inherently parallel

Explanation:

Streams operations are not inherently parallel. Streams can be processed in parallel or sequentially, depending on how they are invoked (using parallelStream() or stream()). Other statements are true characteristics of Java Streams.


Comments