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());
- [2, 4, 6]
- [4, 16, 36]
- [1, 9, 25]
- [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
- Filter keeps even numbers: 2, 4, 6.
- Map squares each: 4, 16, 36.
- 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, 9, 25]
- [1, 3, 5]
- [4, 16]
- [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
- Filter odd numbers: 1, 3, 5.
- Square each.
- 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()?
- 15
- 14
- 10
- 9
Answer: C. 10
Explanation
range excludes the upper bound, so it sums 1+2+3+4 = 10.
Step-by-step solution
- range(1,5) gives 1,2,3,4.
- 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);
}
- [2, 3] and the loop finishes with no error at all
- [1, 3]
- ConcurrentModificationException
- 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
i is Integer, so remove(Object) is called.- The list's modCount changes.
- 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();
- The second call throws IllegalStateException
- Both calls return 2 because the stream is re-read each time
- The second call returns 0 because the stream is now empty
- 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
- The first
count() is terminal. - The stream is now closed.
- 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));
- 20
- 10
- 24
- 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
- Start with 10.
- Add each element.
- 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, 2=1, 3=1}
- {aa=2, b=2, eee=1}
- {3=1, 2=2, 1=2}
- {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
- Lengths: 2,1,2,1,3.
- Group by length.
- 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?
- map is a terminal operation; flatMap is an intermediate one
- flatMap works only on Optional and never on a Stream
- flatMap flattens nested streams; map does not
- 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
- map on lists gives a stream of lists.
- 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?
- A lambda can use a local variable only if it is effectively final
- A lambda may freely modify any local variable it captures, like an inner block
- A lambda can only use instance fields
- 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
- Lambda captures a local
int c. c++ inside the lambda reassigns it.- 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();
- Prints 1 twice because the stream restarts
- IllegalStateException is thrown on the second count()
- Compilation error
- 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
- First count() consumes the stream.
- Second terminal call checks state.
- 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.