Java Streams MCQ Questions and Answers

1. What is the primary purpose of the Java Stream API?

a) To handle I/O operations
b) To facilitate database connectivity
c) To process collections of objects in a functional style
d) To manage multi-threaded operations

Answer:

c) To process collections of objects in a functional style

Explanation:

The Java Stream API is designed for processing sequences of elements, such as those found in collections, in a functional programming style.

2. Which of these is a characteristic of Java streams?

a) Mutability
b) Reusability
c) Laziness
d) Concurrency

Answer:

c) Laziness

Explanation:

Java streams are lazy, meaning that computation on the source data is only performed when necessary.

3. How do you obtain a stream from a list in Java?

a) list.getStream()
b) list.stream()
c) Stream.of(list)
d) new Stream(list)

Answer:

b) list.stream()

Explanation:

The stream() method is used to create a stream from a collection such as a list.

4. What are terminal operations in the context of Java streams?

a) Operations that start the stream processing
b) Operations that transform a stream
c) Operations that produce a result or a side-effect
d) Operations that return another stream

Answer:

c) Operations that produce a result or a side-effect

Explanation:

Terminal operations in Java streams produce a non-stream result or a side-effect and terminate the stream.

5. Which operation is used to filter elements in a Java stream?

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

Answer:

a) filter()

Explanation:

The filter() operation is used to filter elements in a stream based on a given predicate.

6. What does the map operation do in a stream?

a) Reduces the stream to a single value
b) Filters elements based on a condition
c) Applies a function to each element of the stream
d) Groups elements of the stream

Answer:

c) Applies a function to each element of the stream

Explanation:

The map operation applies a function to each element of the stream, transforming the elements.

7. What is the purpose of the reduce operation in Java streams?

a) To filter out elements
b) To perform a reduction on the elements of the stream
c) To convert a stream into a collection
d) To sort the stream elements

Answer:

b) To perform a reduction on the elements of the stream

Explanation:

The reduce operation combines the elements of the stream into a single result using a specified function.

8. How do you convert a stream into a collection, such as a List?

a) Using the toList() method
b) Using the collect() method with Collectors.toList()
c) By iterating over the stream
d) Using the asList() method

Answer:

b) Using the collect() method with Collectors.toList()

Explanation:

The collect() method, along with Collectors.toList(), is used to convert a stream into a list.

9. Which of these is a valid way to create a stream of specific values?

a) Stream.of(1, 2, 3)
b) new Stream(1, 2, 3)
c) Stream.create(1, 2, 3)
d) Stream.get(1, 2, 3)

Answer:

a) Stream.of(1, 2, 3)

Explanation:

Stream.of() is used to create a stream from a group of specific values.

10. What does the forEach operation do in a stream?

a) Filters each element
b) Performs an action for each element
c) Transforms each element
d) Collects elements into a list

Answer:

b) Performs an action for each element

Explanation:

The forEach operation is used to perform a given action for each element of the stream.

11. What is the result of calling findFirst() on a stream?

a) The first element of the stream
b) The last element of the stream
c) A random element from the stream
d) An Optional describing the first element of the stream

Answer:

d) An Optional describing the first element of the stream

Explanation:

findFirst() returns an Optional describing the first element of the stream, or an empty Optional if the stream is empty.

12. How do you create an infinite stream using iterate method?

a) Stream.iterate(seed, unaryOperator)
b) Stream.infinite(seed, unaryOperator)
c) Stream.loop(seed, unaryOperator)
d) Stream.continuous(seed, unaryOperator)

Answer:

a) Stream.iterate(seed, unaryOperator)

Explanation:

Stream.iterate() is used to create an infinite stream by repeatedly applying a given function to the previous result.

13. What is the purpose of the flatMap operation in streams?

a) To flatten multiple streams into a single stream
b) To map each element to a single value
c) To filter out elements
d) To reduce the stream to a single value

Answer:

a) To flatten multiple streams into a single stream

Explanation:

flatMap transforms each element into a stream and then flattens these streams into a single stream.

14. Which operation is used to sort elements in a stream?

a) sort()
b) sorted()
c) order()
d) arrange()

Answer:

b) sorted()

Explanation:

The sorted() operation is used to sort the elements of a stream.

15. How do you create a stream from an array?

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

Answer:

d) Both b and c

Explanation:

A stream can be created from an array either using Arrays.stream(array) or Stream.of(array).

16. Can a stream be reused after a terminal operation has been applied?

a) Yes
b) No
c) Only if it's a parallel stream
d) Only if it's an infinite stream

Answer:

b) No

Explanation:

Once a terminal operation is applied, the stream can no longer be used. Streams are not reusable.

17. What is a parallel stream in Java?

a) A stream that processes elements in a random order
b) A stream that runs in a single thread
c) A stream that can process elements concurrently in multiple threads
d) A stream that merges two streams into one

Answer:

c) A stream that can process elements concurrently in multiple threads

Explanation:

A parallel stream is capable of processing elements concurrently using multiple threads, potentially offering performance benefits over sequential streams.

18. What does the peek operation do in a stream?

a) Removes an element from the stream
b) Performs an action on each element without modifying it
c) Peeks at the first element without consuming it
d) Extracts a portion of the stream

Answer:

b) Performs an action on each element without modifying it

Explanation:

The peek operation performs a specified action on each element of the stream as it is consumed, without modifying the stream itself.

19. How do you limit the number of elements in a stream?

a) Using the limit() method
b) Using the restrict() method
c) By filtering elements
d) By using the take() method

Answer:

a) Using the limit() method

Explanation:

The limit() method is used to truncate the stream to contain no more than a given number of elements.

20. What is the purpose of the Collectors class in the Java Stream API?

a) To provide utility functions for mathematical operations
b) To create new streams
c) To provide implementations of Collector interface used in the collect operation
d) To collect data from a database

Answer:

c) To provide implementations of Collector interface used in the collect operation

Explanation:

The Collectors class provides various implementations of the Collector interface, which are used in the collect operation to accumulate elements into collections and other types of results.


Comments