Streams & Lambdas Questions with Answers

15 practice questions on Streams & Lambdas (Advanced Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

List.of(1,2,3,4,5,6).stream()
    .filter(n -> n % 2 == 0)
    .map(n -> n * n)
    .collect(Collectors.toList());
  1. [2, 4, 6]
  2. [4, 16, 36]
  3. [1, 9, 25]
  4. [4, 16, 36, 4]

Answer: B. [4, 16, 36]

Explanation

The filter keeps 2, 4, 6 and the map squares them to 4, 16, 36.

Step-by-step solution

  1. Filter keeps even numbers: 2, 4, 6.
  2. Map squares each: 4, 16, 36.
  3. Collect into a list.

Why the other options are wrong

  • A. That list is before the map.
  • C. That would be squares of odd numbers.
  • D. There is no duplicate element.

Question 2Easy

What is the output of this code?

System.out.println(Stream.of(1,2,3,4,5)
  .filter(x -> x % 2 == 1)
  .map(x -> x * x)
  .collect(Collectors.toList()));
  1. [1, 9, 25]
  2. [1, 3, 5]
  3. [4, 16]
  4. [2, 4]

Answer: A. [1, 9, 25]

Explanation

Filter keeps 1, 3, 5, then map squares them to 1, 9, 25.

Step-by-step solution

  1. Filter odd numbers: 1, 3, 5.
  2. Square each.
  3. Collect into list.

Why the other options are wrong

  • B. That is the filter output without squaring.
  • C. That squares even numbers, but evens are filtered out.
  • D. That is the even numbers unchanged.

Question 3Easy

What is the output of IntStream.range(1,5).sum()?

  1. 15
  2. 14
  3. 10
  4. 9

Answer: C. 10

Explanation

range excludes the upper bound, so it sums 1+2+3+4 = 10.

Step-by-step solution

  1. range(1,5) gives 1,2,3,4.
  2. Sum them: 10.

Why the other options are wrong

  • A. 15 is for rangeClosed(1,5) which includes 5.
  • B. 14 is not any sum here.
  • D. 9 is 4+5, not the range.

Tip: rangeClosed includes the end.

Question 4Medium

What does this print?

List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
for (Integer i : list) {
    if (i == 1) list.remove(i);
}
  1. [2, 3] and the loop finishes with no error at all
  2. [1, 3]
  3. ConcurrentModificationException
  4. IndexOutOfBoundsException from the iterator index

Answer: C. ConcurrentModificationException

Explanation

remove(Object) modifies the list while the enhanced for loop's iterator is active, so the next next() detects the change.

Step-by-step solution

  1. i is Integer, so remove(Object) is called.
  2. The list's modCount changes.
  3. The next next() throws.

Why the other options are wrong

  • A. The loop does not complete silently here.
  • B. The removal triggers an exception, not a finished result.
  • D. Index errors are not what the iterator reports.

Tip: Use Iterator.remove or removeIf.

Question 5Medium

What happens here?

Stream<Integer> s = Stream.of(1, 2);
s.count();
s.count();
  1. The second call throws IllegalStateException
  2. Both calls return 2 because the stream is re-read each time
  3. The second call returns 0 because the stream is now empty
  4. It does not compile

Answer: A. The second call throws IllegalStateException

Explanation

A stream can be consumed only once; a terminal operation closes it, so a second terminal call fails at run time.

Step-by-step solution

  1. The first count() is terminal.
  2. The stream is now closed.
  3. Reusing it throws IllegalStateException.

Why the other options are wrong

  • B. A stream is not re-readable.
  • C. It does not reset to empty.
  • D. The code compiles fine; the error is at run time.

Question 6Medium

What does this print?

System.out.println(Stream.of(1,2,3,4).reduce(10, (x, y) -> x + y));
  1. 20
  2. 10
  3. 24
  4. Optional[20]

Answer: A. 20

Explanation

The identity 10 is the start: 10+1+2+3+4 = 20, returned as an int.

Step-by-step solution

  1. Start with 10.
  2. Add each element.
  3. Result 20.

Why the other options are wrong

  • B. 10 would be the identity alone.
  • C. 24 is 1 * 2 * 3 * 4, which multiplies and ignores the identity 10.
  • D. With an identity, reduce returns the value directly, not an Optional.

Question 7Medium

What does the following print?

System.out.println(Stream.of("aa","b","cc","d","eee")
  .collect(Collectors.groupingBy(String::length, Collectors.counting())));
  1. {1=1, 2=1, 3=1}
  2. {aa=2, b=2, eee=1}
  3. {3=1, 2=2, 1=2}
  4. {1=2, 2=2, 3=1}

Answer: D. {1=2, 2=2, 3=1}

Explanation

Lengths are 2,1,2,1,3. Counting per length gives 1 -> 2, 2 -> 2, 3 -> 1.

Step-by-step solution

  1. Lengths: 2,1,2,1,3.
  2. Group by length.
  3. Count each group.

Why the other options are wrong

  • A. Counts are 2 of length 1, not 1.
  • B. Keys are lengths, not the strings.
  • C. HashMap prints integer keys in ascending order here, so 3 does not come first.

Question 8Medium

What is the difference between map and flatMap on a stream?

  1. map is a terminal operation; flatMap is an intermediate one
  2. flatMap works only on Optional and never on a Stream
  3. flatMap flattens nested streams; map does not
  4. They are identical aliases that differ only in name

Answer: C. flatMap flattens nested streams; map does not

Explanation

map turns one element into one element (possibly a collection). flatMap turns one element into a stream and merges them.

Step-by-step solution

  1. map on lists gives a stream of lists.
  2. flatMap(List::stream) merges them to a stream of elements.

Why the other options are wrong

  • A. Both are intermediate.
  • B. flatMap exists on Stream and Optional.
  • D. They differ in whether nested streams are flattened.

Question 9Hard

Which statement about lambdas is correct?

  1. A lambda can use a local variable only if it is effectively final
  2. A lambda may freely modify any local variable it captures, like an inner block
  3. A lambda can only use instance fields
  4. A lambda can be assigned to any interface, even one with several abstract methods

Answer: A. A lambda can use a local variable only if it is effectively final

Explanation

Locals captured by a lambda must not be reassigned (effectively final). Instance and static fields may be modified. The target must be a functional interface.

Step-by-step solution

  1. Lambda captures a local int c.
  2. c++ inside the lambda reassigns it.
  3. The compiler rejects it.

Why the other options are wrong

  • B. Modifying a captured local is a compile error.
  • C. Locals and parameters can be captured too.
  • D. The target needs a single abstract method.

Tip: Use AtomicInteger or an array for counters.

Question 10Hard

What does this print?

Stream<Integer> s = Stream.of(1);
s.count();
s.count();
  1. Prints 1 twice because the stream restarts
  2. IllegalStateException is thrown on the second count()
  3. Compilation error
  4. Returns 0 on the second call since the stream is empty

Answer: B. IllegalStateException is thrown on the second count()

Explanation

A stream can be consumed only once. A second terminal operation throws IllegalStateException: stream has already been operated upon or closed.

Step-by-step solution

  1. First count() consumes the stream.
  2. Second terminal call checks state.
  3. Exception raised.

Why the other options are wrong

  • A. Nothing is printed and a stream cannot be reused.
  • C. It compiles; the problem is at run time.
  • D. It throws and does not return 0.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Advanced Java topics · All topics