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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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()?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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
Post a Comment