CCNA Streams Lambdas Questions

72 of 86 questions · Page 1/2 · Streams Lambdas topic · Answers revealed

1
MCQmedium

A developer wants to create a stream that repeatedly generates random integers. Which method should be used?

A.IntStream.range(0, 100)
B.Stream.of(new Random().nextInt())
C.IntStream.generate(() -> new Random().nextInt())
D.Stream.iterate(0, i -> new Random().nextInt())
AnswerC

This creates an infinite stream of random ints; it's the idiomatic choice for primitive streams.

Why this answer

Option C is correct because `IntStream.generate()` accepts a `Supplier<Integer>` and produces an infinite sequential unordered stream, making it ideal for repeatedly generating random integers. The lambda `() -> new Random().nextInt()` supplies a new random integer each time the stream is evaluated, fulfilling the requirement exactly.

Exam trap

The trap here is that candidates confuse `Stream.generate()` with `Stream.iterate()` or `Stream.of()`, mistakenly thinking that a single random call or a finite range can produce an infinite stream of random values, when only `generate()` with a `Supplier` correctly models repeated independent generation.

How to eliminate wrong answers

Option A is wrong because `IntStream.range(0, 100)` generates a finite stream of integers from 0 to 99, not random values. Option B is wrong because `Stream.of(new Random().nextInt())` creates a single-element stream containing one random integer, not a stream that repeatedly generates random integers. Option D is wrong because `Stream.iterate(0, i -> new Random().nextInt())` produces an infinite stream but the first element is always 0, and the seed value is never used in the generation logic, which is not a clean or intended approach for random generation; additionally, `Stream.iterate` is typically used for sequential transformations, not for independent random suppliers.

2
MCQmedium

A developer is designing a method that processes a stream of Employee objects. The method needs to group employees by department and then, within each department, sort by salary in descending order, and collect the top 3 highest-paid employees per department into a list. Which approach correctly accomplishes this using streams?

A.stream.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen(Collectors.toList(), list -> list.stream().sorted(Comparator.comparing(Employee::getSalary).reversed()).limit(3).collect(Collectors.toList()))))
B.stream.collect(Collectors.toMap(Employee::getDepartment, e -> { List<Employee> l = new ArrayList<>(); l.add(e); return l; }, (l1, l2) -> { l1.addAll(l2); l1.sort(Comparator.comparing(Employee::getSalary).reversed()); return l1.subList(0, Math.min(3, l1.size())); }))
C.stream.sorted(Comparator.comparing(Employee::getSalary).reversed()).collect(Collectors.groupingBy(Employee::getDepartment, Collectors.toList())).values().stream().map(l -> l.subList(0, Math.min(3, l.size()))).collect(Collectors.toList())
D.stream.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.toList())).entrySet().stream().flatMap(e -> e.getValue().stream().sorted(Comparator.comparing(Employee::getSalary).reversed()).limit(3)).collect(Collectors.toList())
AnswerA

This groups by department, then for each group sorts descending and limits to 3, exactly as required.

Why this answer

Option A is correct because it uses `Collectors.groupingBy` to partition employees by department, then applies `Collectors.collectingAndThen` to post-process each department's list. Inside, it sorts the list by salary in descending order, limits to the top 3, and collects them into a new list. This ensures each department's result is independently sorted and truncated.

Exam trap

The trap here is that candidates often sort globally before grouping (Option C) or flatten the result (Option D), failing to realize that per-group sorting and limiting must occur inside the downstream collector to maintain group isolation.

How to eliminate wrong answers

Option B is wrong because `Collectors.toMap` with a merge function is not designed for grouping; it incorrectly mutates the same list across multiple employees, leading to shared state and potential data corruption. Option C is wrong because it sorts the entire stream globally before grouping, which destroys per-department ordering, and then uses `subList` on unsorted lists, which may not contain the top 3 per department. Option D is wrong because it flattens the grouped entries into a single stream after sorting and limiting, producing a flat list of top employees across all departments rather than a list of lists per department.

3
MCQhard

Given: List<String> words = Arrays.asList("apple", "banana", "cherry"); Map<Integer, List<String>> map = words.stream().collect(Collectors.groupingBy(String::length)); System.out.println(map); What is the output?

A.[apple, banana, cherry]
B.{5=[5], 6=[6, 6]}
C.{5=[apple], 6=[banana, cherry]}
D.{apple=[5], banana=[6], cherry=[6]}
AnswerC

Correct grouping.

Why this answer

The `groupingBy` collector groups stream elements by a classifier function, here `String::length`. Each distinct length becomes a key in the map, and the values are lists of strings with that length. "apple" (length 5) maps to key 5, "banana" and "cherry" (both length 6) map to key 6, producing `{5=[apple], 6=[banana, cherry]}`.

Exam trap

The trap here is confusing `groupingBy` with `toMap` or assuming the map keys are the string values themselves, leading candidates to reverse the key-value roles or misrepresent the grouped lists.

How to eliminate wrong answers

Option A is wrong because it shows a flat list, not a map, and ignores the grouping operation. Option B is wrong because it incorrectly uses the length values as list elements instead of the original strings, and duplicates the length 6 value. Option D is wrong because it reverses the key-value relationship, making the strings the keys and their lengths the values, which is not what `groupingBy` does.

4
MCQhard

A company runs a financial application that processes a stream of millions of transaction records daily. Each record is a 'Transaction' object with fields: id, amount, currency, timestamp. The system currently uses a parallel stream to group transactions by currency and compute the sum of amounts per currency, using the following code: Map<String, Double> result = transactions.parallelStream() .collect(Collectors.groupingBy(Transaction::getCurrency, Collectors.summingDouble(Transaction::getAmount))); Recently, performance has degraded significantly. Analysis shows that the stream source is a LinkedList, and the operation involves a large number of distinct currencies (over 1000). The JVM is running on a machine with 4 cores. Which is the best course of action to improve performance?

A.Change the stream source to an ArrayList and use sequential stream.
B.Use a custom thread pool with ForkJoinPool to control parallelism.
C.Increase the parallelism level to 8.
D.Replace groupingBy with a custom concurrent collector using ConcurrentHashMap.
AnswerD

C is correct: a concurrent collector (like groupingByConcurrent) improves parallel performance.

Why this answer

Option C is correct: using groupingByConcurrent is designed for parallel streams and uses ConcurrentHashMap, reducing merge overhead. Option A (sequential stream) would lose parallelism and likely be slower. Option B (increasing parallelism) would add overhead without addressing the source or collector issue.

Option D (custom thread pool) does not fix the core problems of poor splitting and non-concurrent collector.

5
MCQhard

A stream pipeline uses sorted() with a Comparator that calls a thread-unsafe method on each element. The pipeline is parallel. What is the likely outcome?

A.The pipeline may produce incorrect ordering or throw a ConcurrentModificationException.
B.The pipeline will throw an exception because the comparator is stateful.
C.The pipeline will sort correctly because sorted() is a sequential operation.
D.The pipeline will run sequentially because the stream source is not ordered.
AnswerA

Correct: A non-thread-safe comparator can cause data corruption or exceptions due to concurrent access.

Why this answer

The sorted operation in parallel streams uses multiple threads and requires the comparator to be stateless and thread-safe. A thread-unsafe comparator can cause unpredictable results or exceptions.

6
Multi-Selecthard

Which TWO are correct about parallel streams in Java? (Choose TWO.)

Select 2 answers
A.Parallel streams always improve performance over sequential streams.
B.Any lambda expression is safe to use in a parallel stream.
C.Using parallel streams guarantees the order of elements in the result.
D.Parallel streams use the common ForkJoinPool by default.
E.To get correct results, operations should be stateless and non-interfering.
AnswersD, E

Default pool is ForkJoinPool.commonPool().

Why this answer

Option D is correct because parallel streams in Java, by default, use the common ForkJoinPool (ForkJoinPool.commonPool()), which is a shared thread pool designed for parallel task execution. This pool is lazily initialized and has a default parallelism level equal to the number of available processors minus one, as per the ForkJoinPool implementation.

Exam trap

The trap here is that candidates often assume parallel streams are always faster (Option A) or that any lambda is safe (Option B), overlooking the critical requirements of statelessness and non-interference for correctness in parallel execution.

7
MCQeasy

Which statement about the Stream API is true?

A.Parallel streams always improve performance.
B.Streams are always finite.
C.Intermediate operations are executed eagerly.
D.A stream can be consumed only once.
AnswerD

Correct.

Why this answer

Option D is correct because a Stream in Java represents a sequence of elements that can be traversed only once. Once a terminal operation (like collect() or forEach()) is invoked on a stream, the stream is consumed and cannot be reused; attempting to do so throws an IllegalStateException. This one-time consumption is a fundamental design principle of the Stream API, ensuring that operations are applied to a single, immutable pipeline.

Exam trap

Oracle often tests the misconception that streams are reusable like collections, but the trap here is that candidates forget a stream is a one-shot object that cannot be traversed more than once, leading them to incorrectly assume they can call multiple terminal operations on the same stream.

How to eliminate wrong answers

Option A is wrong because parallel streams do not always improve performance; overhead from thread management, splitting, and merging can degrade performance, especially on small datasets or operations with high contention. Option B is wrong because streams can be infinite, as demonstrated by Stream.iterate() or Stream.generate(), which produce unbounded sequences that must be limited by short-circuiting operations like limit(). Option C is wrong because intermediate operations (e.g., filter(), map()) are lazy and are not executed until a terminal operation is invoked; they build a pipeline of operations that are applied only when needed.

8
MCQhard

Given: List<String> words = List.of("apple", "banana", "cherry"); var result = words.stream().filter(s -> s.length() > 5).collect(Collectors.toUnmodifiableList()); What happens if another thread tries to add to result?

A.Compilation error because var cannot be used with streams.
B.UnsupportedOperationException at runtime.
C.The thread successfully adds the element.
D.NullPointerException.
AnswerB

The returned list is unmodifiable; calling add() throws UnsupportedOperationException.

Why this answer

B is correct because `Collectors.toUnmodifiableList()` returns an unmodifiable list, and any attempt to modify it (e.g., via `add()`) throws an `UnsupportedOperationException`. The stream pipeline itself is safe and compiles fine; the issue is the mutability of the resulting collection.

Exam trap

The trap here is that candidates may confuse `Collectors.toUnmodifiableList()` with `Collectors.toList()`, which returns a mutable list, and incorrectly assume the list can be modified by another thread.

How to eliminate wrong answers

Option A is wrong because `var` can be used with streams; it infers the type `List<String>` from the right-hand side, and the code compiles without error. Option C is wrong because the list returned by `Collectors.toUnmodifiableList()` is immutable, so no thread can successfully add an element; any modification attempt throws an exception. Option D is wrong because no `NullPointerException` occurs; the list is non-null and the exception is specifically `UnsupportedOperationException`.

9
MCQhard

Given a list of strings, which of the following will produce a Map<String, Long> counting the occurrences of each string?

A..collect(Collectors.toMap(Function.identity(), String::length))
B..collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
C..collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)))
D..collect(Collectors.toMap(Function.identity(), v->1))
AnswerB

Correct. groupingBy with counting as downstream collector produces a long count per key.

Why this answer

Collectors.groupingBy with Collectors.counting() correctly counts occurrences and returns Map<String, Long>.

10
MCQmedium

A developer wants to find the longest word in a list of strings. Which stream operation should be used?

A.stream().sorted(Comparator.comparingInt(String::length).reversed()).findFirst()
B.stream().min(Comparator.comparingInt(String::length))
C.stream().sorted(Comparator.comparingInt(String::length)).findFirst()
D.stream().max(Comparator.comparingInt(String::length))
AnswerD

Correct.

Why this answer

Option D is correct because the `max()` terminal operation, when combined with a `Comparator` that compares strings by their length, returns the longest word in the stream. The `max()` operation uses the provided comparator to determine the 'largest' element according to the ordering, which in this case is the string with the greatest length.

Exam trap

The trap here is that candidates often confuse `min()` and `max()` or incorrectly assume that sorting and taking the first element is the intended approach, overlooking the direct and efficient `max()` operation.

How to eliminate wrong answers

Option A is wrong because it sorts the stream in descending order of length and then calls `findFirst()`, which is unnecessarily complex and less efficient than `max()`; it also requires the stream to be fully sorted before retrieval. Option B is wrong because `min()` returns the smallest element according to the comparator, which would give the shortest word, not the longest. Option C is wrong because it sorts in ascending order of length and then calls `findFirst()`, which returns the shortest word, not the longest.

11
Matchingmedium

Match each exception class to its category (checked/unchecked).

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Checked exception

Unchecked exception

Checked exception

Unchecked exception

Checked exception

Why these pairings

Checked exceptions must be handled or declared; unchecked extend RuntimeException.

12
MCQhard

Which of the following lambda expressions is syntactically invalid?

A.() -> {return 42;}
B.(a, b) -> a + b
C.(a, b) -> return a + b;
D.(int a, int b) -> a + b
E.(a) -> a * 2
AnswerC

Invalid. 'return' keyword requires a block body (curly braces).

Why this answer

Option C is syntactically invalid because in a lambda expression, the `return` keyword is only allowed inside a block body (with braces). When using an expression body (no braces), the `return` keyword must be omitted, and the expression itself is implicitly returned. Option C incorrectly uses `return` without braces, which is a syntax error.

Exam trap

The trap here is that candidates often confuse the syntax of expression lambdas with block lambdas, mistakenly thinking `return` can be used without braces, or they forget that a single expression without `return` is implicitly returned.

How to eliminate wrong answers

Option A is valid because it uses a block body with braces and a `return` statement, which is syntactically correct. Option B is valid because it uses an expression body (no braces) that implicitly returns the result of `a + b`. Option D is valid because it uses explicit parameter types (`int a, int b`) with an expression body, which is allowed.

Option E is valid because it uses a single parameter with an expression body, and parentheses around a single parameter are optional but allowed.

13
MCQeasy

Which of the following is a terminal operation of the Stream API?

A.map()
B.forEach()
C.filter()
D.peek()
AnswerB

Correct. forEach() is a terminal operation.

Why this answer

Option B (forEach()) is correct because it is a terminal operation in the Stream API. Terminal operations produce a result or side effect and close the stream, meaning no further operations can be performed on the same stream. forEach() consumes each element of the stream and returns void, making it a terminal operation.

Exam trap

The trap here is that candidates often confuse peek() with forEach() because both can perform actions on elements, but peek() is intermediate and does not trigger stream processing, while forEach() is terminal and does.

How to eliminate wrong answers

Option A is wrong because map() is an intermediate operation that returns a new stream after applying a function to each element; it does not close the stream. Option C is wrong because filter() is an intermediate operation that returns a new stream containing only elements that match a predicate; it does not terminate the stream. Option D is wrong because peek() is an intermediate operation designed for debugging purposes, allowing you to view elements without terminating the stream.

14
Multi-Selecthard

Which TWO statements about the Stream API are correct? (Choose two.) A. A stream can be traversed multiple times. B. The peek() method is an intermediate operation. C. The findFirst() method returns an Optional. D. The collect() method is an intermediate operation. E. The map() method returns a stream of the same type.

Select 2 answers
A.The peek() method is an intermediate operation.
B.The collect() method is an intermediate operation.
C.The findFirst() method returns an Optional.
D.A stream can be traversed multiple times.
E.The map() method returns a stream of the same type.
AnswersA, C

Correct.

Why this answer

Option A is correct because the `peek()` method is an intermediate operation in the Stream API. Intermediate operations return a new stream and are lazy; they are not executed until a terminal operation is invoked. `peek()` allows you to inspect elements as they flow through the pipeline without modifying them.

Exam trap

Oracle often tests the distinction between intermediate and terminal operations, and the trap here is that candidates confuse `collect()` (terminal) with an intermediate operation or assume `map()` always returns the same stream type, while in reality it can change the element type via the provided function.

15
Multi-Selectmedium

Which TWO statements about method references are true? (Choose two.)

Select 2 answers
A.Constructor references use the syntax Class::new.
B.Array constructor references are not supported.
C.Instance method references always use the keyword static.
D.A method reference can be used in place of a lambda expression of compatible functional interface.
E.Method references cannot refer to methods declared in the same class.
AnswersA, D

Constructor references follow ClassName::new syntax.

Why this answer

Option A is correct because constructor references use the syntax `Class::new` to refer to a constructor of a class, which can be used with a functional interface whose method matches the constructor's signature. This allows creating instances without explicitly invoking `new`, leveraging the same target type inference as lambda expressions.

Exam trap

The trap here is that candidates often confuse instance method references with static method references, mistakenly thinking the `static` keyword is required, or they overlook that array constructor references are indeed supported via `Type[]::new`.

16
MCQmedium

A company needs to process a stream of orders and filter out orders that are not in the 'SHIPPED' status. Then they want to collect the order IDs into a list. Which of the following correctly uses lambda expressions to achieve this?

A.orders.stream().filter(o -> o.getStatus() != "SHIPPED").map(Order::getId).collect(Collectors.toList())
B.orders.stream().map(Order::getId).filter(id -> id.equals("SHIPPED")).collect(Collectors.toList())
C.orders.stream().filter(Order::getStatus).map(o -> o.getId()).collect(Collectors.toList())
D.orders.stream().filter(o -> o.getStatus().equals("SHIPPED")).map(Order::getId).collect(Collectors.toList())
AnswerD

Correctly filters SHIPPED orders and maps to IDs.

Why this answer

Option D is correct because it first filters the stream to include only orders with status 'SHIPPED' using a lambda expression that calls equals() on the status string, then maps each order to its ID using a method reference, and finally collects the IDs into a list. This correctly uses lambda expressions and the Stream API to achieve the requirement.

Exam trap

Oracle often tests the distinction between using equals() vs == for string comparison in lambda expressions, and the correct order of stream operations (filter before map) to avoid type mismatches or logical errors.

How to eliminate wrong answers

Option A is wrong because it filters orders where the status is NOT equal to 'SHIPPED' (using !=), which is the opposite of what is required. Option B is wrong because it maps orders to IDs first, then tries to filter IDs by comparing them to the string 'SHIPPED', which is a type mismatch and logically incorrect. Option C is wrong because it uses a method reference Order::getStatus as a predicate, but getStatus returns a String, not a boolean, so it does not compile or work as a filter.

17
Multi-Selectmedium

Which THREE of the following are valid ways to create a Stream<String>? (Choose three.)

Select 3 answers
A.Arrays.stream(new String[]{"a", "b"})
B.Stream.of("a", "b")
C.List.of("a", "b").stream()
D."ab".chars()
E.new Stream<String>()
AnswersA, B, C

Arrays.stream for object arrays returns a Stream of the array elements.

Why this answer

Stream.of, Arrays.stream for an array, and List.stream are all valid. String.chars() returns an IntStream, and Stream constructor is not public.

18
MCQeasy

Which functional interface is most appropriate for a lambda that takes a String and returns nothing?

A.Predicate<String>
B.Function<String, Void>
C.Supplier<String>
D.Consumer<String>
AnswerD

Consumer<String> accepts a String and returns void, best suited for side-effect operations.

Why this answer

Option A is correct because Consumer<String> has an accept method that takes a String and returns void. Option B (Function<String, Void>) is not idiomatic; Consumer is designed for side effects.

19
MCQmedium

A method receives a List<String> and needs to transform it to a Map where the key is the first three characters of each string and the value is the string itself. If two strings have the same prefix, the later one should override the earlier. Which collector achieves this?

A.Collectors.groupingBy(s -> s.substring(0,3))
B.Collectors.toMap(s -> s.substring(0,3), Function.identity())
C.Collectors.toConcurrentMap(s -> s.substring(0,3), Function.identity())
D.Collectors.toMap(s -> s.substring(0,3), Function.identity(), (a,b) -> b)
AnswerD

Correct: The merge function (a,b) -> b keeps the later value.

Why this answer

Option D is correct because `Collectors.toMap` with a merge function `(a, b) -> b` ensures that when duplicate keys (first three characters) occur, the later string in the stream order replaces the earlier one. The merge function is invoked on key collision, and returning `b` (the second argument) keeps the later value, satisfying the requirement.

Exam trap

The trap here is that candidates often pick `toMap` without a merge function (Option B) or `groupingBy` (Option A), forgetting that duplicate keys cause exceptions or produce the wrong map type, respectively.

How to eliminate wrong answers

Option A is wrong because `groupingBy` groups all strings with the same prefix into a `List<String>`, not a single string per key, so the result is `Map<String, List<String>>` instead of `Map<String, String>`. Option B is wrong because `toMap` without a merge function throws `IllegalStateException` when duplicate keys are encountered, which will happen if any two strings share the same first three characters. Option C is wrong because `toConcurrentMap` also requires a merge function for duplicate keys and will throw an exception without one; additionally, it is designed for parallel streams and is not necessary for this sequential requirement.

20
MCQhard

A developer implements a reduction using reduce() to concatenate strings from a stream. The code: Optional<String> result = stream.reduce((s1, s2) -> s1.concat(s2)); The operation works but the developer is concerned about performance with large streams. Which change would most likely improve performance?

A.Use StringBuffer with collect() instead.
B.Use a sequential stream.
C.Use parallelStream with reduce.
D.Use reduce with identity "".
AnswerA

StringBuilder (or StringBuffer) as a mutable reduction with collect() reduces intermediate allocations.

Why this answer

Option A is correct because `StringBuffer` is mutable and thread-safe, making it efficient for accumulating strings in a parallel or sequential stream via `collect()`. The `reduce()` operation with `concat()` creates a new `String` object for each pair, leading to O(n) string copying and quadratic time complexity. Using `collect()` with `StringBuffer` avoids this overhead by reusing a mutable buffer.

Exam trap

The trap here is that candidates assume `reduce()` with an identity or parallel streams will fix performance, but the core issue is the immutable nature of `String` concatenation, which `collect()` with a mutable container like `StringBuffer` or `StringBuilder` resolves.

How to eliminate wrong answers

Option B is wrong because using a sequential stream does not address the fundamental inefficiency of `reduce()` with `concat()`, which still creates many intermediate `String` objects. Option C is wrong because `parallelStream` with `reduce()` would introduce thread-safety issues and still suffer from string copying overhead, potentially degrading performance further. Option D is wrong because providing an identity `""` to `reduce()` does not change the underlying concatenation mechanism; it still creates new `String` objects per reduction step.

21
Multi-Selectmedium

Which three are terminal operations of the Stream interface? (Choose three.)

Select 3 answers
A.filter
B.map
C.reduce
D.forEach
E.collect
AnswersC, D, E

reduce is a terminal operation that performs a reduction on the elements.

Why this answer

Option A (forEach), Option C (reduce), and Option E (collect) are terminal operations because they produce a result or side effect and close the stream. Option B (map) and Option D (filter) are intermediate operations; they return a stream and are lazy.

22
MCQeasy

A stream pipeline uses the peek method for debugging. Which statement about peek is correct?

A.peek is a terminal operation that prints elements.
B.peek can only be used with parallel streams.
C.peek is an intermediate operation used for debugging; it is not suitable for production side effects.
D.peek is an intermediate operation, and it is guaranteed to be executed for each element.
AnswerC

Correct: Peek is intended for debugging and should not be used for production side effects because its execution is not guaranteed.

Why this answer

Option C is correct because the `peek` method is an intermediate operation in the Stream API, designed primarily for debugging to observe elements as they flow through the pipeline. It is not intended for production side effects because its execution is not guaranteed for every element (e.g., due to short-circuiting or optimization), and relying on it for stateful operations can lead to unpredictable behavior.

Exam trap

The trap here is that candidates assume `peek` is a terminal operation or that it always executes for every element, confusing it with `forEach` or ignoring the Stream API's lazy evaluation and optimization guarantees.

How to eliminate wrong answers

Option A is wrong because `peek` is an intermediate operation, not a terminal operation; it returns a new stream and does not trigger pipeline execution. Option B is wrong because `peek` can be used with both sequential and parallel streams; it is not restricted to parallel streams. Option D is wrong because `peek` is not guaranteed to be executed for each element; the Stream API may skip `peek` calls due to optimizations like short-circuiting (e.g., with `limit()`) or lazy evaluation, making it unreliable for side effects.

23
MCQeasy

A developer is converting legacy for loops to streams. The legacy code: List<Integer> list = new ArrayList<>(); for (String s : strings) { if (s.length() > 5) { list.add(s.length()); } } They write: List<Integer> list = strings.stream() .filter(s -> s.length() > 5) .map(s -> s.length()) .collect(Collectors.toList()); But it doesn't compile. The error is: 'cannot find symbol: method collect(Collector<Object,?,List<Object>>)'. What is the likely issue?

A.The lambda in filter is incorrectly written; it should be s.length > 5.
B.The map operation returns an IntStream, which does not have a collect method. Use map(s -> s.length()).boxed().collect(...) or mapToInt(...).boxed().
C.The stream should be made unordered to allow the collector to function.
D.Use parallelStream() to enable the collect method.
AnswerB

s.length() returns int, so map produces an IntStream. To collect to List<Integer>, you need to box to Stream<Integer>.

Why this answer

Option B is correct because the stream's type is String, but the pipeline returns an int (or Integer) after map; however, the error suggests the compiler cannot infer the collector type. The problem is that filter returns a Stream<String>, then map returns IntStream because s.length() returns int, and collect is not available on IntStream; they need to box it or use mapToInt then boxed. Option A is false because the lambda is valid.

Option C is false because unordered would not affect compilation. Option D is false because the issue is not parallel.

24
MCQmedium

Refer to the exhibit. What is the output?

A.10
B.null
C.-1
D.Optional.empty
AnswerC

Correct.

Why this answer

The filter n > 100 yields an empty stream. findFirst returns an empty Optional. orElse(-1) returns -1.

25
MCQeasy

The code fails to compile. What is the reason?

A.The collect method is not applicable for a Stream<Character>.
B.The method reference String::chars is not a valid method reference.
C.The chars() method returns a Stream<Character>, but flatMap requires a function that returns a Stream<Integer>.
D.The flatMap method requires a function that returns a Stream, but String::chars returns an IntStream.
AnswerD

flatMap expects a Function returning a Stream<? extends R>, but chars() returns IntStream, which is not a Stream.

Why this answer

Option D is correct because the `chars()` method on `String` returns an `IntStream`, not a `Stream<Character>`. The `flatMap` method on a `Stream<String>` expects a function that returns a `Stream<?>`, but `String::chars` returns an `IntStream`, which is a primitive stream and not a subtype of `Stream<?>`. This type mismatch causes a compilation error.

Exam trap

The trap here is that candidates often assume `String::chars` returns a `Stream<Character>` because it deals with characters, but it actually returns an `IntStream`, leading to a type mismatch when used with `flatMap`.

How to eliminate wrong answers

Option A is wrong because `collect` is applicable to `Stream<Character>`; the issue is not with `collect` but with the intermediate `flatMap` operation. Option B is wrong because `String::chars` is a valid method reference; it refers to the `chars()` method of `String`, which returns an `IntStream`. Option C is wrong because `chars()` returns an `IntStream`, not a `Stream<Character>`, and `flatMap` requires a function that returns a `Stream`, but the mismatch is that `IntStream` is not a `Stream<Integer>`.

26
MCQmedium

Refer to the exhibit. What is the output?

A.30
B.0
C.12
D.20
AnswerC

Correct. (2*2)+(4*2)=4+8=12.

Why this answer

The stream filters even numbers (2,4) then doubles them (4,8) and sums to 12.

27
MCQeasy

Which code will successfully produce an Optional<Integer> that contains the maximum value from a list of integers?

A.B and C both produce Optional<Integer>.
B.list.stream().collect(Collectors.maxBy(Integer::compareTo))
C.list.stream().max()
D.list.stream().max(Integer::compareTo)
AnswerA

Both B and C compile and produce an Optional<Integer> with the maximum element.

Why this answer

Option A is correct because both B and C produce an Optional<Integer> containing the maximum value from the list. Option B uses Collectors.maxBy with Integer::compareTo, which returns an Optional<Integer>. Option C uses the max() method on an IntStream (obtained via list.stream().mapToInt(Integer::intValue)), which returns an OptionalInt, not Optional<Integer>.

However, the question asks for an Optional<Integer>, and Option C's max() on a Stream<Integer> (not IntStream) returns Optional<Integer> directly. Since the question's context implies a Stream<Integer>, Option C is valid. Option D is incorrect because max(Integer::compareTo) is not a valid method on Stream; the correct method is max(Comparator) which returns Optional<T>, but the syntax in D is missing the comparator argument properly—it should be max(Comparator.naturalOrder()) or similar, not max(Integer::compareTo) as a method reference without a comparator context.

Thus, only B and C produce Optional<Integer>.

Exam trap

Oracle often tests the distinction between Stream<T>.max(Comparator) and IntStream.max() (which returns OptionalInt), and the fact that max() without arguments on a Stream<T> requires the elements to implement Comparable, while max(Integer::compareTo) is a common mistake because Integer::compareTo is a method reference that does not implement Comparator directly—candidates may incorrectly assume it can be passed as a comparator argument.

How to eliminate wrong answers

Option B is correct because Collectors.maxBy(Integer::compareTo) returns an Optional<Integer> containing the maximum element. Option C is correct because list.stream().max() on a Stream<Integer> uses natural ordering (Integer implements Comparable) and returns Optional<Integer>. Option D is wrong because list.stream().max(Integer::compareTo) is syntactically invalid; max() expects a Comparator argument, but Integer::compareTo is a method reference that returns an int, not a Comparator—the correct form would be max(Integer::compareTo) if Integer::compareTo were a Comparator, but it is not; the proper comparator is Comparator.naturalOrder() or Comparator.comparingInt(Integer::intValue).

28
MCQmedium

A developer wants to collect elements from a stream into an immutable List. Which collector should be used?

A.Collectors.toList()
B.Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)
C.Collectors.toList().stream().collect(Collectors.toUnmodifiableList())
D.Collectors.toUnmodifiableList()
AnswerD

Correct: Returns an unmodifiable list.

Why this answer

Option D is correct because `Collectors.toUnmodifiableList()` directly returns a collector that accumulates elements into an immutable `List`. This was introduced in Java 10 and guarantees that the resulting list cannot be modified, throwing `UnsupportedOperationException` on any mutation attempt.

Exam trap

The trap here is that candidates may think `Collectors.toList()` returns an immutable list (since it's often used in read-only contexts), or they may overcomplicate the solution by chaining collectors when a direct method exists.

How to eliminate wrong answers

Option A is wrong because `Collectors.toList()` returns a mutable `List` (typically an `ArrayList`), not an immutable one. Option B is wrong because although `Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)` does produce an unmodifiable list, it is unnecessarily verbose and less efficient than the direct `toUnmodifiableList()` method; it also wraps a mutable list rather than creating a truly immutable list from the start. Option C is wrong because `Collectors.toList().stream().collect(Collectors.toUnmodifiableList())` is syntactically invalid — `Collectors.toList()` returns a `Collector`, not a `Stream`, so calling `.stream()` on it is a compile-time error.

29
MCQeasy

What does the following code print? List<Integer> list = List.of(1, 2, 3, 4, 5); list.stream().filter(i -> i % 2 == 0).forEach(System.out::print);

A.No output
B.12345
C.135
D.24
AnswerD

Correct.

Why this answer

The correct answer is D because the stream filters only even numbers (i % 2 == 0) from the list, producing 2 and 4, which are then printed sequentially via forEach. The filter operation retains elements where the predicate is true, so only 2 and 4 satisfy the condition.

Exam trap

The trap here is that candidates often confuse the filter predicate (i % 2 == 0) with selecting odd numbers, or they forget that filter retains matching elements rather than removing them, leading to selection of option C or B.

How to eliminate wrong answers

Option A is wrong because the stream pipeline does produce output; the filter does not eliminate all elements, so there is output. Option B is wrong because it prints all numbers (1,2,3,4,5), which would occur if no filter were applied, but the filter removes odd numbers. Option C is wrong because it prints odd numbers (1,3,5), which is the opposite of the filter condition (i % 2 == 0 selects even numbers).

30
Multi-Selecteasy

Which TWO of the following are characteristics of a well-designed lambda expression? (Choose two.)

Select 2 answers
A.Is stateless
B.Throws exceptions that are not declared in the functional interface's abstract method
C.Uses the same variable name as a parameter in the enclosing scope (shadowing)
D.Uses only effectively final variables from the enclosing scope
E.Modifies local variables of the enclosing method
AnswersA, D

Stateless lambdas are safer, especially in parallel streams, as they avoid race conditions.

Why this answer

A well-designed lambda should be stateless and use effectively final variables to avoid side effects and ensure predictability.

31
MCQhard

Assuming the code runs in a multithreaded environment, which statement best describes the behavior?

A.The code will compile but throw an exception at runtime because of the use of parallel stream with stateful lambda.
B.It prints the doubled numbers in order (2, 4, 6, 8) and results contains [1,2,3,4] in that order.
C.It may produce a ConcurrentModificationException because results is modified while iterating.
D.It prints the doubled numbers in a non-deterministic order and results may contain missing or duplicate entries.
AnswerD

Correct: Parallel stream processes elements concurrently, so the order of map execution is non-deterministic. The shared results list is not thread-safe, leading to data corruption.

Why this answer

The lambda in map is stateful (modifying results list) and not thread-safe. This can cause race conditions and non-deterministic output. Additionally, forEachOrdered does not make the preceding stateful operation safe.

32
MCQhard

A company runs a Java 17 microservice that processes real-time financial transactions. The application receives a large number of transactions per second, each with a timestamp, amount, and type. The current implementation uses a sequential stream to filter and aggregate transactions into a Map<TransactionType, DoubleSummaryStatistics>. The team observes high latency and CPU spikes during peak loads. They suspect the stream pipeline is inefficient. The pipeline code is: Map<TransactionType, DoubleSummaryStatistics> stats = transactions.stream() .filter(t -> t.getTimestamp().isAfter(Instant.now().minusSeconds(60))) .collect(Collectors.groupingBy(Transaction::getType, Collectors.summarizingDouble(Transaction::getAmount))); The transactions list is an ArrayList that is frequently modified by other threads (adding new transactions). The system has multiple CPU cores available. Which of the following changes is the MOST effective way to improve performance while maintaining correctness?

A.Use .stream().parallel() with a custom thread pool and ensure the stream source is not modified during operation.
B.Use .parallelStream() on a snapshot (new ArrayList<>(transactions)) and keep the same collector.
C.Replace ArrayList with CopyOnWriteArrayList and use .parallelStream() with the same collector.
D.Replace the collector with Collectors.groupingByConcurrent() and use .parallelStream() directly on the original list.
AnswerB

Correct. The snapshot eliminates concurrent modification issues, and parallel processing improves throughput.

Why this answer

Creating a snapshot (new ArrayList<>(transactions)) ensures the stream operates on a consistent, immutable view, avoiding ConcurrentModificationException. Using parallelStream then leverages multiple cores. The groupingBy collector works correctly in parallel because it has a combiner.

This combination balances performance and safety.

33
MCQmedium

A team implements a stream pipeline that processes a large dataset in parallel. They use a stateful lambda expression inside the map operation to maintain a count. What is the most likely outcome?

A.The pipeline will produce correct results if the stateful lambda is synchronized.
B.The pipeline will produce non-deterministic results due to race conditions.
C.The pipeline will throw a ConcurrentModificationException.
D.The pipeline will always produce the same result regardless of parallelism.
AnswerB

Stateful lambdas in parallel streams are discouraged because they introduce shared mutable state that leads to race conditions and unpredictable results.

Why this answer

Option B is correct because using a stateful lambda (one that mutates shared mutable state) inside a parallel stream's map operation introduces race conditions. The map operation is intended to be stateless and non-interfering; when multiple threads concurrently update a shared counter without proper synchronization, the result becomes non-deterministic and unpredictable.

Exam trap

The trap here is that candidates assume synchronization or thread-safety fixes the issue, but the Streams API's contract requires stateless lambdas for correctness in parallel pipelines, and synchronization does not restore deterministic behavior or guarantee correct results.

How to eliminate wrong answers

Option A is wrong because even if the stateful lambda is synchronized, the synchronization will cause severe contention and may still produce incorrect results if the lambda is not properly thread-safe or if the ordering of updates is not guaranteed; the Java Streams API explicitly discourages stateful lambdas in parallel pipelines. Option C is wrong because ConcurrentModificationException is thrown when a collection is structurally modified while being iterated, not from a stateful lambda in a map operation that merely updates a counter. Option D is wrong because the pipeline will not always produce the same result; the non-determinism arises from the unpredictable interleaving of thread updates to the shared mutable state.

34
MCQhard

A stream pipeline uses groupingBy with a downstream collector to count occurrences. The developer expects a Map<Integer, Long> but gets a compilation error. Which is the correct way to write the collector?

A.Collectors.toMap(Function.identity(), e -> 1L, Long::sum)
B.Collectors.groupingBy(Function.identity(), Collectors.count())
C.Collectors.groupingBy(Function.identity(), Collectors.counting())
D.Collectors.groupingBy(Function.identity(), Collectors.summingLong(e -> 1))
AnswerC

Correct: groupingBy with counting() returns Map<Integer, Long>.

Why this answer

Collectors.groupingBy(Function, Collectors.counting()) returns a Map<K, Long>.

35
MCQhard

A team is developing a real-time data processing pipeline that reads sensor data from a message queue. The pipeline uses a flatMap operation that calls an external geocoding service for each sensor reading. The external service has a rate limit of 10 requests per second and is slow (150ms average response time). The current code: sensorStream.parallelStream() .flatMap(reading -> getGeocode(reading).stream()) .forEach(system.out::println); The application is overloaded because parallel stream fires many concurrent requests, exceeding the rate limit and causing failures. They need to process all sensor data but must respect the rate limit. Which approach should they use?

A.Use a sequential stream with a custom rate limiter (e.g., a semaphore) that blocks when the limit is reached.
B.Reduce the parallelism level by using a custom thread pool with a fixed number of threads.
C.Use filter to drop some sensor readings to reduce the load.
D.Use a sequential stream and hope the processing completes within the time window.
AnswerA

A sequential stream combined with a rate limiter ensures requests are sent at a controlled pace, respecting the external service limits.

Why this answer

Option D is correct because using a sequential stream with a controlled batch size and throttling via a rate limiter (like a semaphore or Guava's RateLimiter) is the only way to reliably respect the external service's rate limit while not losing data. Options A and B only reduce parallelism but do not enforce a specific rate. Option C is wrong because it discards data.

36
MCQeasy

A lambda expression that takes two integers and returns a boolean indicating whether the first is greater than the second is best represented by which functional interface?

A.BiFunction<Integer, Integer, Boolean>
B.Predicate<Integer>
C.BinaryOperator<Boolean>
D.BiPredicate<Integer, Integer>
AnswerD

Correct. BiPredicate<Integer, Integer> is the functional interface for two input arguments returning a boolean.

Why this answer

BiPredicate is designed for two arguments and returns a boolean. Option B correctly identifies this.

37
MCQmedium

A developer uses a parallel stream to process a large collection and wants to collect results into a List while preserving encounter order. Which of the following collectors will guarantee order preservation?

A.collect(Collectors.toUnmodifiableList())
B.collect(Collectors.toList())
C.collect(Collectors.toMap(Function.identity(), v->v))
D.collect(ArrayList::new, List::add, List::addAll)
E.collect(Collectors.toCollection(TreeSet::new))
AnswerD

This custom collector preserves encounter order because it adds elements sequentially in the accumulator and merges lists in order.

Why this answer

A custom collector using ArrayList::new, List::add, and List::addAll preserves insertion order even in parallel streams. Other collectors may not guarantee order.

38
Multi-Selecthard

Which TWO statements are true about the Stream API and lambda expressions in Java 17?

Select 2 answers
A.The Stream API does not modify the original data source.
B.Lambda expressions can capture variables that are effectively final.
C.A stream can be consumed by multiple terminal operations sequentially.
D.Intermediate operations on a stream are executed eagerly.
E.Lambda expressions introduce a new level of scope, similar to an anonymous inner class.
AnswersA, B

Streams operate on a view of the data; they do not modify the underlying collection or array.

Why this answer

Option A is correct because the Stream API is designed to work on a view of the data, not the data itself. Operations like filter, map, and collect produce new streams or results without altering the original collection, array, or I/O source. This non-interference guarantee is a core principle of the Stream API, ensuring safe parallel execution and functional programming style.

Exam trap

The trap here is that candidates often confuse the lazy evaluation of intermediate operations with eager execution, or mistakenly believe a stream can be reused after a terminal operation, leading them to select options C or D.

39
Multi-Selecthard

Which THREE of the following are valid ways to create an infinite stream? (Choose three.)

Select 3 answers
A.IntStream.generate(() -> 0)
B.Stream.iterate(1, n -> n+1)
C.IntStream.range(0, Integer.MAX_VALUE)
D.Stream.generate(Math::random)
E.Stream.of(1,2,3)
AnswersA, B, D

Generates an infinite IntStream of zeros.

Why this answer

Option A is correct because `IntStream.generate(() -> 0)` uses a supplier that always returns 0, producing an infinite stream of zeros. The `generate` method creates an unordered infinite stream by repeatedly invoking the supplier, with no termination condition.

Exam trap

The trap here is that candidates mistakenly think `IntStream.range(0, Integer.MAX_VALUE)` is infinite because the upper bound is large, but it is actually a finite stream that terminates after producing Integer.MAX_VALUE elements.

40
MCQmedium

A developer is refactoring a legacy codebase to use streams. The original code iterates over a list of 'Order' objects, filters orders with status 'PENDING', sorts them by date, and collects the order IDs into a set. Which stream pipeline correctly replaces this logic?

A.orders.stream().filter(o->o.getStatus().equals("PENDING")).sorted(Comparator.comparing(Order::getDate)).collect(Collectors.toSet())
B.orders.stream().filter(o->o.getStatus().equals("PENDING")).map(Order::getId).sorted().collect(Collectors.toSet())
C.orders.stream().sorted(Comparator.comparing(Order::getDate)).filter(o->o.getStatus().equals("PENDING")).map(Order::getId).collect(Collectors.toSet())
D.orders.stream().filter(o->o.getStatus().equals("PENDING")).sorted(Comparator.comparing(Order::getDate)).map(Order::getId).collect(Collectors.toSet())
AnswerD

B is correct: filters, sorts by date, maps to ID, collects to Set.

Why this answer

Option B is correct because it filters pending orders, sorts by date, then maps to IDs and collects to a set. Option A is wrong because it collects Order objects instead of IDs. Option C is wrong because it sorts IDs, not dates.

Option D is wrong because it sorts all orders before filtering, which is less efficient and may not match the original behavior.

41
Multi-Selectmedium

Which TWO are valid lambda expressions? (Choose two.) A. (int a, int b) -> a + b B. a, b -> a + b C. (a, b) -> a + b D. (a, b) -> { a + b; } E. (int a, b) -> a + b

Select 2 answers
A.a, b -> a + b
B.(a, b) -> a + b
C.(int a, int b) -> a + b
D.(a, b) -> { a + b; }
E.(int a, b) -> a + b
AnswersB, C

Valid lambda with inferred types.

Why this answer

Option B is correct because a lambda expression with inferred parameter types can omit the parameter types and parentheses when there is exactly one parameter, but here there are two parameters, so parentheses are required. Option C is correct because it explicitly declares the types of both parameters, which is valid syntax for a lambda expression that returns the sum of the two integers.

Exam trap

Oracle often tests the rule that parentheses are mandatory for multiple parameters in a lambda, and that mixing explicit and inferred parameter types is illegal, causing candidates to mistakenly accept options like A or E.

42
MCQeasy

A developer needs to concatenate two Stream<String> into one. Which approach is most idiomatic?

A.stream1.flatMap(s -> stream2)
B.new StreamBuilder().add(stream1).add(stream2).build()
C.Stream.concat(stream1, stream2)
D.stream1.merge(stream2)
AnswerC

Correct: Static method that returns a concatenated stream.

Why this answer

Stream.concat is the standard method to concatenate two streams.

43
MCQeasy

A developer writes the following code using the Stream API: List<String> list = List.of("a", "b", "c"); String result = list.stream().reduce("", (s1, s2) -> s1 + s2); System.out.println(result); What is the output?

A.abc
B.a
C.Optional[abc]
D.cba
AnswerA

Correct concatenation.

Why this answer

The correct answer is A because the `reduce` method with an identity value (`""`) and a binary operator (`(s1, s2) -> s1 + s2`) accumulates the stream elements in encounter order. Starting from the identity, it concatenates each element: `"" + "a" = "a"`, then `"a" + "b" = "ab"`, then `"ab" + "c" = "abc"`. The result is a `String`, not an `Optional`, because an identity is provided.

Exam trap

The trap here is that candidates often confuse the `reduce` overloads, mistakenly thinking that all `reduce` operations return an `Optional`, or they forget that the identity value is included in the accumulation, leading them to expect an `Optional` wrapper or a reversed order.

How to eliminate wrong answers

Option B is wrong because it suggests only the first element is output, but `reduce` with a binary operator processes all elements sequentially, not just the first. Option C is wrong because when an identity value is supplied to `reduce`, the return type is the same as the identity type (`String`), not `Optional<String>`; `Optional` is only returned when using the overloaded `reduce` without an identity. Option D is wrong because the stream's encounter order is preserved from the list `["a", "b", "c"]`, and the accumulator concatenates in that order, not reversed; reversing would require a different operation like `sorted(Comparator.reverseOrder())`.

44
MCQmedium

What is the result of executing this code?

A.An exception is thrown at runtime.
B.bbc
C.Compilation error
D.baaabc
AnswerA

IllegalStateException is thrown when a terminal operation is called on an already-consumed stream.

Why this answer

Option A is correct because the code attempts to call `findFirst()` on a `Stream<String>` that has already been consumed by a previous terminal operation (`forEach`). In Java, streams are single-use; after a terminal operation is executed, the stream is closed and cannot be reused. Any attempt to perform another terminal operation on the same stream throws an `IllegalStateException` at runtime.

Exam trap

The trap here is that candidates often forget that streams are single-use and incorrectly assume that chaining multiple terminal operations on the same stream is allowed, leading them to pick a concatenated output like 'baaabc' instead of recognizing the runtime exception.

How to eliminate wrong answers

Option B is wrong because it assumes the stream can be reused and that `findFirst()` would return 'b', but the stream is already closed after `forEach`. Option C is wrong because the code compiles successfully; the error occurs only at runtime. Option D is wrong because it incorrectly concatenates the results of both terminal operations, which is impossible since the second operation never executes.

45
MCQhard

A team is implementing a parallel stream to process a large dataset. They notice that the operation is slower than expected. Which change is most likely to improve performance?

A.Use a sequential stream.
B.Use a custom Collector instead of reduce.
C.Increase the parallelism level by setting -Djava.util.concurrent.ForkJoinPool.common.parallelism=100.
D.Ensure the stream source is an ArrayList and the operation is stateless and associative.
AnswerD

D is correct: ArrayList splits well, and stateless/associative operations reduce merge cost.

Why this answer

Option D is correct because for parallel streams, using a well-suited source like ArrayList and ensuring operations are stateless and associative minimizes overhead. Option A may reduce throughput if the dataset is large. Option B can cause thread contention and degrade performance.

Option C is not generally a performance fix.

46
MCQhard

A developer writes a stream pipeline that uses flatMap and filter but notices that the intermediate streams created by flatMap are never garbage-collected early, causing memory pressure. What is the most effective optimization to reduce memory usage?

A.Move the filter operation before the flatMap operation.
B.Replace flatMap with map and collect into a list.
C.Use a terminal operation like collect with a custom collector to process elements eagerly.
D.Use a parallel stream to process elements concurrently.
AnswerA

Filtering early reduces the number of elements fed into flatMap, thereby reducing the number of intermediate streams created.

Why this answer

Option A is correct because moving the filter operation before flatMap reduces the number of elements passed to flatMap, thereby reducing the number of intermediate streams created and their memory footprint. Streams are lazy, so filter applied early discards unwanted elements before flatMap ever sees them, which directly lowers memory pressure from intermediate stream objects.

Exam trap

The trap here is that candidates often assume filter order doesn't matter because streams are lazy, but they overlook that intermediate stream objects from flatMap are not garbage-collected until the pipeline completes, making early filtering critical for memory efficiency.

How to eliminate wrong answers

Option B is wrong because replacing flatMap with map and collect into a list would eagerly materialize all elements into a collection, increasing memory usage rather than reducing it. Option C is wrong because using a terminal operation like collect with a custom collector does not change the lazy nature of intermediate streams; it still processes all elements and cannot force early garbage collection of intermediate stream objects. Option D is wrong because using a parallel stream does not reduce memory usage from intermediate streams; it may even increase memory overhead due to thread coordination and splitting overhead.

47
MCQeasy

A lambda that takes a String and returns its length is assigned to which functional interface?

A.UnaryOperator<String>
B.Consumer<String>
C.Predicate<String>
D.Supplier<Integer>
E.Function<String, Integer>
AnswerE

Correct. This functional interface accepts a String and returns an Integer.

Why this answer

Function<String, Integer> takes a String and returns an Integer. The lambda s -> s.length() matches this signature.

48
MCQmedium

A developer writes: List<Integer> list = List.of(1, 2, 3); Optional<Integer> opt = list.stream().reduce((a, b) -> a + b); System.out.println(opt.get()); What is the result?

A.6
B.Optional[6]
C.Optional.empty
D.NoSuchElementException
AnswerA

Correct output.

Why this answer

The `reduce` method with a single argument (a `BinaryOperator`) returns an `Optional` because the stream might be empty. Here, the stream contains three integers, so the reduction computes `1 + 2 = 3`, then `3 + 3 = 6`, yielding `Optional[6]`. Calling `get()` on a non-empty `Optional` returns the contained value `6`, which is printed.

Exam trap

The trap here is that candidates may forget that `reduce` with one argument returns an `Optional` and incorrectly assume the output is `Optional[6]` or that `get()` throws an exception, overlooking that the stream is non-empty.

How to eliminate wrong answers

Option B is wrong because `opt.get()` extracts the value from the `Optional`, so the output is `6`, not `Optional[6]`. Option C is wrong because the stream is not empty; it has three elements, so the `Optional` is not empty. Option D is wrong because `NoSuchElementException` would only be thrown if `get()` were called on an empty `Optional`, which does not occur here.

49
MCQmedium

A company uses a large dataset of customer orders. They want to compute statistics: total orders, average amount, and maximum amount per city. They write: Map<String, IntSummaryStatistics> stats = orders.stream() .collect(Collectors.groupingBy(Order::getCity, Collectors.summarizingInt(Order::getAmount))); The code works but is slower than expected when run on a large dataset. They suspect the grouping operation is not taking advantage of parallelism. They want to improve performance by making the collector concurrent. Which change is correct?

A.Use Collectors.groupingByConcurrent(Order::getCity, Collectors.summarizingInt(Order::getAmount)).
B.Use parallelStream() instead of stream().
C.Keep the code but supply a ConcurrentHashMap as the map supplier to groupingBy.
D.Replace collect with forEach and manually update a ConcurrentHashMap.
AnswerA

groupingByConcurrent returns a concurrent Collector that uses a ConcurrentHashMap, optimized for parallel streams.

Why this answer

Option D is correct because groupingByConcurrent returns a ConcurrentMap (e.g., ConcurrentHashMap) and is designed for parallel stream processing, allowing concurrent insertion. Option A is wrong because parallelStream() alone does not make the collector concurrent; the collector must also support it. Option B is wrong because forEach is not recommended for this aggregation.

Option C is wrong because changing the map supplier to ConcurrentHashMap without using groupingByConcurrent may still cause thread-safety issues.

50
MCQeasy

A method returns an Optional<String>. The developer wants to transform the value inside the Optional to uppercase and print it, but only if present. Which best-practice approach uses streams?

A.opt.map(String::toUpperCase).ifPresent(System.out::println);
B.opt.ifPresent(s -> System.out.println(s.toUpperCase()));
C.opt.stream().map(String::toUpperCase).forEach(System.out::println);
D.Stream.of(opt).map(o -> o.get().toUpperCase()).forEach(System.out::println);
AnswerC

This converts Optional to a stream of one or zero elements, applies transformation, and prints. It is a stream-based best practice.

Why this answer

Option C is correct because it converts the Optional to a Stream using opt.stream(), then applies map(String::toUpperCase) to transform the value, and finally uses forEach(System.out::println) to print it only if present. This approach leverages the Stream API's lazy evaluation and functional pipeline, which is the best practice for integrating Optional with streams.

Exam trap

The trap here is that candidates may overlook the 'uses streams' constraint and pick A or B, which are valid Optional operations but not stream-based, or pick D which misuses Stream.of to wrap the Optional instead of converting it.

How to eliminate wrong answers

Option A is wrong because opt.map(String::toUpperCase).ifPresent(System.out::println) uses Optional's map and ifPresent, not streams; the question explicitly asks for a 'streams' approach. Option B is wrong because opt.ifPresent(s -> System.out.println(s.toUpperCase())) is a direct Optional consumer, not a stream-based solution. Option D is wrong because Stream.of(opt) creates a stream of Optional, not the contained value, and calling o.get() is unsafe (throws NoSuchElementException if empty) and violates the 'only if present' requirement.

51
MCQhard

Given a list of integers, a developer wants to compute the sum of squares of numbers greater than 10. The following code is written: int sum = list.stream().filter(i -> i>10).mapToInt(i->i*i).sum(); But the sum is incorrect. What is the most likely reason?

A.The mapToInt operation is not allowed after filter.
B.The stream is not sorted.
C.The list might contain nulls causing NullPointerException.
D.The filter should be before mapToInt, but it is after.
E.The sum() method returns an OptionalInt and must be orElse(0).
AnswerC

If any element is null, unboxing in i > 10 throws NPE, making the result incorrect if caught or halting execution.

Why this answer

Option C is correct because if the list contains null elements, the lambda expression `i -> i > 10` will attempt to unbox a null `Integer` to an `int`, causing a `NullPointerException` at runtime. The stream pipeline itself is syntactically valid, but the presence of nulls in the source list is a common pitfall when using primitive-specialized operations like `mapToInt` after a filter that does not guard against nulls.

Exam trap

Oracle often tests the misconception that `sum()` returns an `OptionalInt` (like `reduce` does) or that the order of `filter` and `mapToInt` is reversed, but the real trap is the silent NullPointerException from unboxing nulls in a lambda.

How to eliminate wrong answers

Option A is wrong because `mapToInt` is perfectly allowed after `filter`; the filter returns a `Stream<Integer>`, and `mapToInt` can be called on any `Stream` of objects to produce an `IntStream`. Option B is wrong because sorting is irrelevant to computing the sum of squares; the stream does not need to be sorted for any of the operations used. Option D is wrong because the order of `filter` before `mapToInt` is correct; filtering first reduces the number of elements to square, which is efficient and logically sound.

Option E is wrong because `sum()` on an `IntStream` returns a primitive `int`, not an `OptionalInt`; `OptionalInt` is returned only by reduction operations like `reduce()` or terminal operations like `findFirst()` on an `IntStream`.

52
Multi-Selecteasy

Which two statements are true about the peek method of the Stream API? (Choose two.)

Select 2 answers
A.It is primarily used for debugging purposes.
B.It is an intermediate operation.
C.It can be used to modify the elements of the stream.
D.It is a terminal operation.
E.It can be used to transform the elements into a new value.
AnswersA, B

peek is intended to aid in debugging by allowing observation of elements as they pass through the pipeline.

Why this answer

Option A is correct because peek is an intermediate operation that returns a new stream. Option D is correct because peek is primarily used for debugging to see elements as they flow through the pipeline. Option B is incorrect because peek is not a terminal operation.

Option C is incorrect because peek should not modify state; it is intended for debugging only. Option E is incorrect because peek does not transform elements; it only performs an action on each element.

53
MCQmedium

A company processes financial transactions. Each transaction is represented by a Transaction object with fields: amount (double), currency (String), and type (String). The requirement is to compute the total amount of all transactions of type 'SALE' in USD. The transactions are stored in a List<Transaction>. Which code correctly accomplishes this using streams?

A.transactions.stream().mapToDouble(Transaction::amount).filter(t -> t.type().equals("SALE")).sum()
B.transactions.stream().filter(t -> t.type().equals("SALE")).filter(t -> t.currency().equals("USD")).mapToDouble(Transaction::amount).sum()
C.transactions.stream().map(Transaction::amount).reduce(0.0, (a, b) -> a + b)
D.transactions.stream().filter(t -> t.type().equals("SALE") && t.currency().equals("USD")).mapToDouble(Transaction::amount).sum()
AnswerD

Correctly filters by type and currency, then sums amounts.

Why this answer

Option D correctly filters transactions to only those with type 'SALE' and currency 'USD', then maps each to its amount as a double, and sums them using sum(). This satisfies the requirement exactly: total amount of 'SALE' transactions in USD.

Exam trap

Oracle often tests the order of stream operations — specifically that filter must come before mapToDouble when the filter condition depends on object fields, otherwise the stream loses access to the original object type.

How to eliminate wrong answers

Option A is wrong because it maps to double before filtering, so the filter cannot access the Transaction object's type field — it would attempt to filter on a double, causing a compilation error. Option B is wrong because it filters for both 'SALE' and 'USD', but the requirement only asks for 'SALE' transactions in USD — this is actually correct in logic but the filter order is inefficient; however, the real issue is that it is not the most direct answer and D is more concise. Option C is wrong because it maps all transactions to their amounts without any filtering, summing all amounts regardless of type or currency, which does not meet the requirement.

54
Multi-Selecthard

Which THREE statements about lambda expressions are true? (Choose three.) A. A lambda expression can be assigned to a functional interface variable. B. A lambda expression can access final or effectively final local variables. C. A lambda expression can throw any checked exception. D. A lambda expression can be used to create an instance of an abstract class. E. A lambda expression can be used to implement multiple abstract methods.

Select 3 answers
A.A lambda expression can throw any checked exception.
B.A lambda expression can be assigned to a functional interface variable.
C.A lambda expression can access final or effectively final local variables.
D.A lambda expression can be used to implement multiple abstract methods.
E.A lambda expression can be used to create an instance of an abstract class.
AnswersA, B, C

Correct if the functional interface's abstract method declares it.

Why this answer

Option A is correct because a lambda expression can be assigned to a variable of a functional interface type. The lambda provides the implementation for the single abstract method of that interface, and the assignment is valid as long as the lambda's parameter types and return type match the method signature.

Exam trap

The trap here is that candidates confuse the ability to assign a lambda to a functional interface variable (true) with the ability to implement multiple abstract methods (false), or they mistakenly think lambdas can instantiate abstract classes, which is not supported.

55
MCQmedium

Refer to the exhibit. What is the output?

A.0
B.15
C.10
D.5
AnswerB

Correct.

Why this answer

The reduce method starts with identity 0 and applies the accumulator function to sum all elements: 0+1=1, 1+2=3, 3+3=6, 6+4=10, 10+5=15.

56
Multi-Selecteasy

Which two statements about the Stream API are true? (Choose two.)

Select 2 answers
A.The 'reduce()' operation can produce a result of a different type than the stream elements.
B.The 'flatMap()' operation is a terminal operation.
C.The 'findFirst()' operation is a short-circuiting terminal operation.
D.The 'collect()' operation is an intermediate operation.
E.Streams can be reused after a terminal operation.
AnswersA, C

C is true: the three-argument reduce can transform the type.

Why this answer

Options B and C are true. B: findFirst() is short-circuiting and terminal. C: reduce() with identity, accumulator, and combiner can produce a different type.

A is false because streams cannot be reused after a terminal operation. D is false because collect() is terminal. E is false because flatMap() is intermediate.

57
MCQmedium

What is the output of the code above?

A.18
B.15
C.9
D.27
AnswerA

Correct: (2*3)+(4*3)=6+12=18.

Why this answer

The filter keeps only even numbers: 2 and 4. mapToInt multiplies by 3: 6 and 12. Sum = 18.

58
Multi-Selecthard

Which THREE are valid method references in Java 17? (Choose three.)

Select 3 answers
A.String::new
B.String::isEmpty
C.null::equals
D.Math::random
E.this::toString
AnswersB, D, E

Valid: Reference to instance method isEmpty of an arbitrary String object.

Why this answer

All three are valid: static method reference (String::isEmpty), instance method of an arbitrary object (list::add, but that's for a specific instance? Actually, list::add is an instance method of a particular list object, that's valid. String::isEmpty is a static method reference? No, isEmpty is an instance method of String. But when used as String::isEmpty, it's a reference to an instance method of an arbitrary object of a particular type.

It is valid. this::method is also valid. SomeClass::new is a constructor reference.

59
MCQhard

Refer to the exhibit. What is the output?

A.{apple=2, banana=2, orange=2}
B.{1=[apple, banana, orange], 2=[apple, banana], 3=[apple]}
C.{apple=3, banana=2, orange=1}
D.{apple=3, banana=1, orange=1}
AnswerC

Correct.

Why this answer

The stream collects a map from each word to its count. Apple appears 3 times, banana 2, orange 1. Output: {apple=3, banana=2, orange=1}.

60
Multi-Selecteasy

Which TWO are terminal operations on the Stream interface? (Choose two.)

Select 2 answers
A.count()
B.forEach()
C.filter()
D.map()
E.sorted()
AnswersA, B

Terminal operation that returns the count of elements.

Why this answer

A is correct because `count()` is a terminal operation that reduces the stream to a single `long` value by counting the number of elements after processing the pipeline. It triggers the stream pipeline execution and consumes the stream, making it a terminal operation per the `Stream` interface specification.

Exam trap

The trap here is that candidates often confuse intermediate operations (like `filter`, `map`, `sorted`) with terminal operations because they are commonly chained together, but only terminal operations produce a non-stream result or side effect and close the stream.

61
MCQmedium

A Java team is processing a large dataset with parallel streams. They notice inconsistent results due to non-atomic operations on shared mutable state. Which approach should they use to ensure thread-safety while maximizing performance?

A.Use the collect method with a concurrent Collector such as toConcurrentMap().
B.Use forEach with AtomicInteger and update atomically.
C.Use synchronized blocks inside the lambda expression.
D.Use ConcurrentHashMap for accumulation, but collect using toList().
AnswerA

Concurrent Collectors are designed for parallel reduction, using internal synchronization and efficient merging.

Why this answer

Option C is correct because using a concurrent Collector (like toConcurrentMap or groupingByConcurrent) ensures both thread-safety and efficiency in parallel processing. Option A is wrong because synchronized blocks inside lambdas can cause contention and undermine parallelism. Option B is wrong because ConcurrentHashMap alone does not guarantee atomicity of compound operations.

Option D is wrong because forEach with AtomicInteger still requires careful coordination and may not be optimal.

62
MCQeasy

A developer wants to count how many strings in a list have length greater than 5. Which is the correct implementation?

A.list.stream().mapToLong(s -> s.length() > 5).sum()
B.list.stream().filter(s -> s.length() > 5).count()
C.list.stream().count(s -> s.length() > 5)
D.list.stream().collect(Collectors.counting(s -> s.length() > 5))
AnswerB

Correct: filter then count gives number of elements satisfying predicate.

Why this answer

Option B is correct because the Stream API provides the `filter` method to select elements matching a predicate, and `count` returns the number of elements in the resulting stream. This directly counts strings with length greater than 5 without needing any intermediate mapping or collection.

Exam trap

The trap here is that candidates often confuse `count()` with a method that accepts a predicate, or mistakenly try to map booleans to numeric values using `mapToLong`, forgetting that `mapToLong` requires a `ToLongFunction` returning a primitive `long`, not a `boolean`.

How to eliminate wrong answers

Option A is wrong because `mapToLong` expects a `ToLongFunction` that returns a primitive `long`, but the lambda `s -> s.length() > 5` returns a `boolean`, causing a compilation error; even if corrected, `sum()` would not count booleans. Option C is wrong because `count()` does not accept a predicate; it takes no arguments and returns the total number of elements in the stream. Option D is wrong because `Collectors.counting()` takes no predicate argument; it simply counts all elements, and passing a predicate would cause a compilation error.

63
Multi-Selectmedium

Which TWO are valid ways to obtain a Stream from a List<String> named 'list'? (Choose two.)

Select 2 answers
A.list.stream()
B.Stream.of(list)
C.Arrays.stream(list.toArray())
D.new Stream<>(list)
E.list.parallelStream()
AnswersA, E

Correct: Returns a sequential stream of the list elements.

Why this answer

Option A is correct because the `List` interface provides the `stream()` method, which returns a sequential `Stream` over the elements of the list. This is the standard way to obtain a stream from any `Collection` in Java.

Exam trap

The trap here is that candidates may confuse `Stream.of(list)` with `list.stream()`, not realizing that `Stream.of()` treats the collection as a single element, and may also incorrectly think `Stream` can be instantiated with `new` like a regular class.

64
MCQhard

Refer to the exhibit. What is the output?

A.{3=[Bob], 5=[Alice, David, Charlie]}
B.{5=[Alice, David], 3=[Bob], 7=[Charlie]}
C.{3=[Bob], 5=[Alice, David], 7=[Charlie]}
D.{3=[Bob], 5=[Alice], 7=[Charlie, David]}
AnswerC

Correct.

Why this answer

groupingBy creates a map where keys are name lengths. Alice (5), Bob (3), Charlie (7), David (5). So map: {3=[Bob], 5=[Alice, David], 7=[Charlie]}.

65
MCQeasy

A developer is processing a stream of strings and needs to create a map where the key is the string length and the value is a list of strings of that length. They write the following code: Map<Integer, List<String>> map = strings.stream() .collect(Collectors.groupingBy(String::length)); The code works correctly in a sequential stream, but when they switch to parallelStream, they notice that sometimes the map contains fewer keys than expected. They suspect that the issue is related to the default map implementation used by groupingBy. What is the most likely cause and the correct fix?

A.Provide a custom thread-safe map factory to groupingBy, e.g., ConcurrentHashMap::new.
B.Remove parallelStream() and use stream() to avoid concurrency issues.
C.Replace groupingBy with groupingByConcurrent.
D.The classifier function (String::length) is not threadsafe.
AnswerC

groupingByConcurrent uses a ConcurrentHashMap, which is thread-safe and allows efficient parallel accumulation.

Why this answer

Option C is correct because the default groupingBy uses a map that is not thread-safe for parallel streams. Using a concurrent collector like groupingByConcurrent ensures thread-safe accumulation. Option A is wrong because the classifier is fine.

Option B is wrong because the stream is already parallel; there's no need to switch back to sequential. Option D is wrong because the issue is not with the classifier.

66
MCQhard

A financial services company has a microservice that processes trade confirmations. The service receives a stream of Trade objects (with fields: id (long), symbol (String), quantity (int), price (double)) and needs to compute the total value (quantity * price) for each symbol, but only for trades with quantity > 0 and price > 0. The result should be a Map<String, Double> mapping symbol to total value. The current implementation uses a for loop with manual aggregation, but it is error-prone and difficult to parallelize. The team decides to refactor using the Stream API. The DataSource provides a Stream<Trade> trades(). The code must be efficient and handle large datasets. Which approach best meets these requirements?

A.trades().filter(t -> t.quantity() > 0 && t.price() > 0).collect(Collectors.groupingBy(Trade::symbol, Collectors.summingDouble(t -> t.quantity() * t.price())))
B.trades().filter(t -> t.quantity() > 0 && t.price() > 0).collect(Collectors.toMap(Trade::symbol, t -> t.quantity() * t.price(), Double::sum))
C.trades().filter(t -> t.quantity() > 0 && t.price() > 0).collect(Collectors.toMap(Trade::symbol, t -> t.quantity() * t.price(), (v1, v2) -> v1 + v2))
D.trades().filter(t -> t.quantity() > 0 && t.price() > 0).reduce(new HashMap<>(), (map, t) -> { map.merge(t.symbol(), t.quantity() * t.price(), Double::sum); return map; }, (m1, m2) -> { m1.putAll(m2); return m1; })
AnswerA

Correct and idiomatic.

Why this answer

Option A is correct because it uses `Collectors.groupingBy` with a downstream `Collectors.summingDouble` collector, which is the idiomatic and efficient way to group trades by symbol and sum their computed values (quantity * price) after filtering out invalid trades. This approach is concise, leverages the Stream API's built-in parallelization support, and avoids manual accumulation or mutable state issues.

Exam trap

The trap here is that candidates may choose a `toMap` or `reduce` variant thinking they are more flexible, but they overlook the subtle correctness issues with mutable reduction or the need for a proper merge function in parallel streams, while `groupingBy` with a downstream collector is the intended pattern for this scenario.

How to eliminate wrong answers

Option B is wrong because `Collectors.toMap` with a merge function `Double::sum` is functionally equivalent but less idiomatic for grouping and summing; it may throw an `IllegalStateException` if duplicate keys are encountered before the merge function is applied, though the merge function handles it here, the groupingBy approach is more readable and directly expresses the intent. Option C is wrong because it uses a lambda `(v1, v2) -> v1 + v2` instead of a method reference, which is functionally identical but less concise and not a technical error; however, the primary issue is that `toMap` does not guarantee the same ordering or efficiency as `groupingBy` for large datasets, and it is not the standard pattern for this operation. Option D is wrong because it uses `reduce` with a mutable `HashMap` and an incorrect combiner that calls `putAll`, which overwrites values instead of merging them properly; this violates the immutability requirement of `reduce` and can produce incorrect results in parallel streams, as the combiner does not sum values from both maps.

67
Multi-Selecteasy

Which TWO of the following are terminal operations on a stream? (Choose two.)

Select 2 answers
A.forEach
B.distinct
C.reduce
D.filter
E.map
AnswersA, C

forEach is a terminal operation that performs an action on each element.

Why this answer

Option A is correct because `forEach` is a terminal operation that consumes the stream, applying the provided action to each element and producing a side effect without returning a new stream. Option C is correct because `reduce` is a terminal operation that combines stream elements into a single result using an associative accumulation function, terminating the stream pipeline.

Exam trap

The trap here is that candidates often confuse intermediate operations like `distinct`, `filter`, and `map` with terminal operations because they also process elements, but they do not produce a final result or side effect that terminates the stream.

68
MCQmedium

A data analytics platform processes user activity logs. Each log entry is a LogRecord with fields: userId (int), action (String), timestamp (long). The requirement is to find the top 3 most active users (by count of actions) in the last hour. The logs are stored in a List<LogRecord> logs. The current solution sorts all records by userId and counts manually, but it's slow. The team decides to use streams with parallel processing. Which code correctly identifies the top 3 users?

A.logs.parallelStream().filter(l -> l.timestamp() > System.currentTimeMillis() - 3600000).collect(Collectors.groupingBy(LogRecord::userId, Collectors.counting())).entrySet().stream().sorted(Map.Entry.<Long, Long>comparingByValue().reversed()).limit(3).collect(Collectors.toList())
B.logs.parallelStream().filter(l -> l.timestamp() > System.currentTimeMillis() - 3600000).collect(Collectors.groupingBy(LogRecord::userId, Collectors.counting())).entrySet().stream().sorted(Map.Entry.comparingByKey()).limit(3).collect(Collectors.toList())
C.logs.parallelStream().collect(Collectors.groupingBy(LogRecord::userId, Collectors.counting())).entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).limit(3).collect(Collectors.toList())
D.logs.parallelStream().filter(l -> l.timestamp() > System.currentTimeMillis() - 3600000).collect(Collectors.groupingBy(LogRecord::action, Collectors.counting())).entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).limit(3).collect(Collectors.toList())
AnswerA

Correct.

Why this answer

Option A is correct because it first filters logs to only those within the last hour, then groups by userId and counts actions, sorts the resulting map entries by count in descending order, limits to the top 3, and collects the result. This uses parallelStream() for performance and correctly applies the required logic.

Exam trap

The trap here is that candidates may forget to filter by timestamp (as in option C) or group by the wrong field (as in option D), or sort by the wrong comparator (as in option B), leading to incorrect results that still compile and run.

How to eliminate wrong answers

Option B is wrong because it sorts by key (userId) instead of by value (count), so it returns the top 3 users by userId order, not by activity count. Option C is wrong because it does not filter by timestamp, so it counts all actions regardless of time, failing the 'last hour' requirement. Option D is wrong because it groups by action instead of userId, so it counts actions per action type, not per user, which does not identify the most active users.

69
MCQhard

A developer needs to process a stream of integers and collect the results into a Map<Integer, List<Integer>> where keys are the integers themselves and values are lists containing the number and its square. Which collector should be used?

A.Collectors.toMap(Function.identity(), i -> Arrays.asList(i, i * i), (v1, v2) -> v1)
B.Collectors.groupingBy(Function.identity(), Collectors.mapping(i -> i * i, Collectors.toList()))
C.Collectors.toMap(Function.identity(), i -> Arrays.asList(i, i * i), (v1, v2) -> v1, HashMap::new)
D.Collectors.toMap(Function.identity(), i -> i * i)
AnswerC

Correctly creates map with list values, handles duplicates by keeping first.

Why this answer

Option C is correct because it uses the four-argument overload of `Collectors.toMap()`: a key mapper (`Function.identity()`), a value mapper (a lambda that creates a `List<Integer>` containing the number and its square), a merge function (`(v1, v2) -> v1`) to handle duplicate keys (which won't occur here since keys are unique integers), and a `HashMap::new` supplier to ensure the map type is explicitly `HashMap`. This satisfies the requirement of producing a `Map<Integer, List<Integer>>` where each key maps to a list of the number and its square.

Exam trap

Oracle often tests the distinction between the three-argument and four-argument `toMap()` overloads, trapping candidates who omit the map supplier or who confuse `groupingBy()` with `toMap()` when the requirement is to store both the original element and a derived value in the map value.

How to eliminate wrong answers

Option A is wrong because `Collectors.toMap(Function.identity(), i -> Arrays.asList(i, i * i), (v1, v2) -> v1)` lacks a map supplier, so it returns a default `HashMap` but will throw `IllegalStateException` at runtime if duplicate keys are encountered (the merge function is only used for merging, not for preventing the exception when keys are truly duplicate; however, here keys are unique so it would work, but the question expects the four-argument version to guarantee the correct map type and avoid ambiguity). Option B is wrong because `Collectors.groupingBy(Function.identity(), Collectors.mapping(i -> i * i, Collectors.toList()))` produces a `Map<Integer, List<Integer>>` where each value is a list of squares only, not a list containing both the number and its square. Option D is wrong because `Collectors.toMap(Function.identity(), i -> i * i)` produces a `Map<Integer, Integer>` with only the square as the value, not a `List<Integer>`.

70
MCQhard

A developer writes the following code to print a list of strings in order: list.stream().map(s -> s.toUpperCase()).forEach(System.out::print). They want to parallelize the processing but must preserve the output order. Which change is correct and most appropriate?

A.list.parallelStream().map(s -> s.toUpperCase()).forEach(System.out::print);
B.list.parallelStream().map(s -> s.toUpperCase()).forEachOrdered(System.out::print);
C.list.stream().parallel().map(s -> { synchronized(System.out) { return s.toUpperCase(); } }).forEach(System.out::print);
D.list.stream().parallel().map(s -> s.toUpperCase()).sequential().forEach(System.out::print);
AnswerB

forEachOrdered ensures that processing respects the encounter order, even in a parallel stream.

Why this answer

Option C is correct because parallelStream() returns a parallel stream, and forEachOrdered guarantees encounter order while still allowing parallel processing (though ordering may reduce some parallelism). Option A is wrong because forEach does not preserve order in parallel streams. Option B is wrong because sequential() would lose parallelism.

Option D is wrong because synchronized is unnecessary and reduces performance.

71
MCQeasy

A developer wants to compute the product of all even numbers in a stream of integers. Which of the following correctly implements this using streams?

A.reduce(1, (a, b) -> (a % 2 == 0) ? a * b : a)
B.filter(n -> n % 2 == 0).reduce(0, (a, b) -> a * b)
C.reduce(1, (a, b) -> a * b)
D.filter(n -> n % 2 == 0).reduce(1, (a, b) -> a * b)
AnswerD

Correct. Filter ensures only even numbers are processed, then reduction multiplies them using identity 1.

Why this answer

Option C is correct because it first filters even numbers (n -> n % 2 == 0), then reduces with identity 1 and multiplication (a, b) -> a * b. Option A is wrong because reduce does not conditionally multiply; it applies the same function to all elements. Option B is wrong because it multiplies all numbers, not just even.

Option D is wrong because identity 0 would make the product always 0.

72
MCQhard

Refer to the exhibit. A developer runs the code and gets an IllegalStateException on the second forEach. Which statement explains why?

A.The filter operation is not lazy.
B.The map operation modifies the source list.
C.The stream is not closed after the first terminal operation.
D.The stream has already been operated upon or closed.
AnswerD

Correct. A stream cannot be reused after a terminal operation.

Why this answer

Streams can only be consumed once. After the terminal operation forEach is called on stream, the stream is closed. Any subsequent operation on the same stream reference throws IllegalStateException.

Page 1 of 2 · 86 questions totalNext →

Ready to test yourself?

Try a timed practice session using only Streams Lambdas questions.