Java Stream API Quiz - Multiple Choice Questions (MCQ)

The Stream API introduced in Java 8 revolutionized the way we handle collections and perform operations on them. It provides a powerful and concise way to manipulate data using functional programming principles.

In this blog post, we present a Java 8 Stream API Quiz consisting of multiple-choice questions (MCQ) to assess your understanding of the Stream API concepts, including stream creation, operations, terminal operations, and functional interfaces. Let's dive into the quiz and test your knowledge of the Java 8 Stream API!

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills 

1. What is a Stream in the context of the Java 8 Stream API?

a) A data structure for storing elements.
b) A sequence of data elements that can be processed in parallel or sequentially.
c) A container for holding collections.
d) A type of exception thrown during runtime.

Answer:

Option (b) is correct.

Explanation:

In the Java 8 Stream API, a Stream represents a sequence of data elements that can be processed in parallel or sequentially. It allows for efficient and functional-style operations on collections of data. Streams provide a way to perform operations on a collection of elements, such as filtering, mapping, and reducing. They enable concise and expressive code for data manipulation tasks.

2. Which interface represents a sequence of elements on which one or more operations can be performed?

a) Collection
b) Map
c) Stream
d) List

Answer:

c) Stream

Explanation:

The Stream interface represents a sequence of objects, allowing various operations to be performed.

3. Which of the following is not a terminal operation in the Stream API?

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

Answer:

b) map

Explanation:

map is an intermediate operation that transforms the elements of the stream.

4. Which method is used to create a Stream from a collection in Java 8?

a) stream()
b) parallelStream()
c) getStream()
d) createStream()

Answer:

a) stream()

Explanation:

The stream() method is used to create a Stream from a collection in Java 8. It is available on any collection object and returns a sequential Stream.

5. What is the purpose of the filter() operation in the Stream API?

a) To transform elements by applying a function.
b) To remove duplicate elements from the stream.
c) To perform an action on each element of the stream.
d) To filter elements based on a condition.

Answer:

d) To filter elements based on a condition.

Explanation:

The filter() operation is used to filter elements based on a condition in the Stream API. It takes a Predicate as an argument and returns a new Stream containing only the elements that satisfy the given condition.

6. What is the purpose of the map() operation in the Stream API?

a) To perform an action on each element of the stream.
b) To remove duplicate elements from the stream.
c) To transform elements by applying a function.
d) To filter elements based on a condition.

Answer:

c) To transform elements by applying a function.

Explanation:

map is an intermediate operation that transforms the elements of the stream.

7. What is the difference between intermediate and terminal operations in the Stream API?

a) Intermediate operations return a new Stream, while terminal operations produce a final result or a side effect.
b) Intermediate operations are executed lazily, while terminal operations are executed eagerly.
c) Intermediate operations can be chained together, while terminal operations cannot.
d) Intermediate operations transform the Stream elements, while terminal operations perform a reduction or aggregation.

Answer:

a) Intermediate operations return a new Stream, while terminal operations produce a final result or a side effect.

Explanation:

Intermediate operations in the Stream API return a new Stream that can be further processed, while terminal operations produce a final result or perform a side effect.

8. What is the output of the following Stream operation?

Stream.of(1, 2, 3, 4).map(i -> i * 2).collect(Collectors.toList())
a) [1, 2, 3, 4]
b) [2, 4, 6, 8]
c) [2, 3, 4, 5]
d) [1, 3, 5, 7]

Answer:

b) [2, 4, 6, 8]

Explanation:

The map() operation multiplies each element of the Stream by 2, resulting in the output [2, 4, 6, 8].

9. What is the purpose of the collect() method in the Stream API?

a) To transform the elements of a Stream.
b) To filter the elements in a Stream based on a predicate.
c) To perform a reduction operation on the Stream elements.
d) To accumulate the elements of a Stream into a collection or a summary result.

Answer:

d) To accumulate the elements of a Stream into a collection or a summary result.

Explanation:

The collect() method is used to accumulate the elements of a Stream into a collection or a summary result.

10. Which of the following can turn a Stream into a Stream>?

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

Answer:

a) map

Explanation:

Using map with a function that returns a Stream would result in a Stream>.

11. Which method will return a stream without any duplicates?

a) map
b) distinct
c) peek
d) skip

Answer:

b) distinct

Explanation:

The distinct method returns a stream consisting of the distinct elements.

12. Which of the following operations is stateful?

a) map
b) filter
c) sorted
d) peek

Answer:

c) sorted

Explanation:

A stateful operation, such as sorted, may incorporate state from previously seen elements when processing new elements.

13. Which method can produce a Stream from an array?

a) Stream.generate()
b) Stream.iterate()
c) Arrays.stream()
d) Stream.of()

Answer:

c) Arrays.stream()

Explanation:

Arrays.stream() method provides a way to obtain a stream from an array.

14. What will the following code return?

Stream<String> stream = Stream.of("a", "b", "c");
Optional<String> result = stream.filter(s -> s.equals("d")).findFirst();
a) "d"
b) "a"
c) Optional.empty
d) null

Answer:

c) Optional.empty

Explanation:

Since there's no "d" in the stream, findFirst() will return Optional.empty.

15. Which operation can transform a Stream into a Stream representing the length of each string?

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

Answer:

a) map

Explanation:

The map operation can transform the type of elements in a stream. For instance, using map(s -> s.length()) will achieve the transformation in the question.

16. Which method in the Stream API can be used to obtain the average of IntStream?

a) average()
b) mean()
c) median()
d) mode()

Answer:

a) average()

Explanation:

The average() method is specifically available for IntStream, LongStream, and DoubleStream and it returns an OptionalDouble representing the average of the elements.

Conclusion

Congratulations on completing the Java 8 Stream API Quiz! We hope this quiz challenged your knowledge and enhanced your understanding of the Stream API concepts in Java 8. The Stream API provides powerful tools for manipulating and processing collections with concise and functional-style code. 

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

Keep practicing and exploring more advanced Stream operations to become proficient in leveraging the Java 8 Stream API's capabilities.

Comments