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

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

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

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

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()

5. What type of operation is Stream.filter?

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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()

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

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

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

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

Comments