Oracle Java OCP Streams and Lambda Expressions Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Thames
T
Thames
Community Contributor
Quizzes Created: 11982 | Total Attempts: 187,298
| Attempts: 19 | Questions: 20 | Updated: Aug 15, 2026
Please wait...
Question 1 / 21
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. Which of the following correctly creates a stream from an array?

Explanation

`Arrays.stream(array)` creates a stream whose elements come from the array. Arrays do not have a `stream()` method, and `Stream` is an interface, so it cannot be instantiated with `new`. `Stream.of(array)` already returns a stream, so calling `.stream()` on it is invalid.

Submit
Please wait...
About This Quiz
Oracle Java OCP Streams and Lambda Expressions Quiz - Quiz

Master Java streams and lambda expressions, core features of modern Java development. This quiz evaluates your understanding of functional programming, stream operations, method references, and lambda syntax. Essential for OCP certification and professional Java development.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. True or False: Streams are lazy, meaning intermediate operations are executed only when a terminal operation is called.

Explanation

True. Stream intermediate operations, such as `filter` and `map`, are not performed when they are declared. They run only when a terminal operation, such as `forEach` or `collect`, is invoked, allowing streams to process elements efficiently as needed.

Submit

3. What does the skip(n) operation do?

Explanation

The `skip(n)` operation ignores the first `n` elements in a sequence and returns the remaining elements. For example, skipping 2 elements from `[1, 2, 3, 4]` returns `[3, 4]`.

Submit

4. True or False: The findFirst() operation is a terminal operation.

Explanation

True. In Java Streams, `findFirst()` is a terminal operation because it initiates execution of the stream pipeline and returns a result—an `Optional` containing the first element, or an empty `Optional` if the stream has no elements. After it runs, the stream cannot be reused.

Submit

5. True or False: The reduce() method is a terminal operation.

Explanation

True. In Java Streams, `reduce()` processes the stream’s elements to combine them into a single result, such as a sum or concatenated value. Because it produces a result and ends the stream pipeline, it is a terminal operation. After it runs, the stream cannot be reused.

Submit

6. What does the peek() operation do in a stream?

Explanation

In a Java stream, `peek()` applies a specified action to each element as it passes through the pipeline, typically for inspection or debugging. It does not change the elements or remove them, and it remains an intermediate operation, so it runs when a terminal operation processes the stream.

Submit

7. What is the output of the following code? List nums = Arrays.asList(1,2,3); nums.stream().map(x -> x*2).forEach(System.out::println);

Explanation

The `map` operation multiplies each element in the list by 2, producing a stream containing `2`, `4`, and `6`. Then `forEach(System.out::println)` prints each value on a separate line.

Submit

8. What does the flatMap() operation do in a stream?

Explanation

flatMap() transforms each element into a stream of zero or more elements, then combines those streams into a single stream. This is useful for flattening nested collections or processing multiple values produced from each input element.

Submit

9. Which stream operation is terminal and collects stream elements into a List?

Explanation

`collect(Collectors.toList())` is a terminal stream operation that processes the stream’s elements and gathers them into a `List`. Terminal operations trigger stream processing and produce a result, while `map()` and `filter()` are intermediate operations that return a stream.

Submit

10. Which functional interface accepts a single parameter and returns a boolean?

Explanation

`Predicate` is a functional interface that takes one argument of type `T` and returns a `boolean`, typically to test whether the argument meets a condition. By contrast, `Function` returns a value, `Consumer` accepts an argument without returning a result, and `Supplier` provides a value without taking an argument.

Submit

11. True or False: A stream can be reused after calling a terminal operation.

Explanation

A Java stream is intended for a single use. A terminal operation consumes it, so subsequent operations on that same stream are not permitted and typically throw an IllegalStateException. To process the data again, create a new stream from the source or otherwise obtain a fresh stream, rather than attempting to reuse the consumed one.

Submit

12. Which of the following is a valid method reference syntax?

Explanation

In Java, the double colon (`::`) operator denotes a method reference. `System.out::println` refers to the `println` method of the `PrintStream` object `System.out`, allowing it to be passed where a compatible functional interface is expected. The dot, hash, and arrow forms shown do not use valid Java method-reference syntax.

Submit

13. Which functional interface is used as the target type for a lambda expression with no parameters and no return value?

Explanation

`Runnable` is a functional interface whose single abstract method, `run()`, takes no parameters and returns no value, matching the lambda’s signature. `Supplier` returns a value, while `Consumer` and `Function` require an input parameter.

Submit

14. What is the result of stream().limit(3) on a stream of 10 elements?

Explanation

`limit(3)` restricts the stream to at most three elements, keeping them in encounter order. For a stream of 10 elements, it returns the first 3 and discards the rest from the resulting stream.

Submit

15. Which stream operation returns a boolean indicating if all elements match a predicate?

Explanation

`allMatch()` returns a boolean indicating whether every element in the stream satisfies the given predicate. It may stop early and return `false` as soon as it finds an element that does not match.

Submit

16. True or False: Lambda expressions can access non-final local variables.

Explanation

Java lambda expressions may capture local variables only when they are final or effectively final—that is, assigned once and never changed after initialization. A local variable that is modified later is not effectively final, so the lambda cannot access it. This restriction keeps captured values consistent even when the lambda executes later or in another thread.

Submit

17. What does the sorted() operation do without parameters?

Explanation

Without any parameters, `sorted()` arranges the items in their natural order—typically ascending order for numbers and alphabetical order for strings—and returns a new sorted list without changing the original iterable.

Submit

18. Which method creates a stream of primitive integers from start to end?

Explanation

`IntStream.range(1, 10)` creates a stream of primitive `int` values from 1 up to, but not including, 10. `Stream.range` does not exist, while `Stream.of` creates a stream of boxed `Integer` values rather than a primitive integer stream.

Submit

19. Which functional interface represents a function that takes one parameter and returns a value?

Explanation

`Function` represents an operation that accepts one argument of type `T` and returns a result of type `R`. Its single abstract method is `apply(T)`. By contrast, `Consumer` accepts a value without returning one, `Supplier` returns a value without taking an argument, and `Predicate` returns a boolean.

Submit

20. What is the correct syntax for a lambda expression that takes two integers and returns their sum?

Explanation

In Java, lambda expressions use the `->` operator to separate parameters from the expression body. Here, `(a, b)` defines the two parameters, and `a + b` returns their sum.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following correctly creates a stream from an array?
True or False: Streams are lazy, meaning intermediate operations are...
What does the skip(n) operation do?
True or False: The findFirst() operation is a terminal operation.
True or False: The reduce() method is a terminal operation.
What does the peek() operation do in a stream?
What is the output of the following code? List nums =...
What does the flatMap() operation do in a stream?
Which stream operation is terminal and collects stream elements into a...
Which functional interface accepts a single parameter and returns a...
True or False: A stream can be reused after calling a terminal...
Which of the following is a valid method reference syntax?
Which functional interface is used as the target type for a lambda...
What is the result of stream().limit(3) on a stream of 10 elements?
Which stream operation returns a boolean indicating if all elements...
True or False: Lambda expressions can access non-final local...
What does the sorted() operation do without parameters?
Which method creates a stream of primitive integers from start to end?
Which functional interface represents a function that takes one...
What is the correct syntax for a lambda expression that takes two...
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!