Java Stream Match and FindFirst Results

Answer Correct answer: B — The stream operations print false true true Optional[NewYear] based on the match predicates and findFirst result.

Given the code fragment: What is the result? - image

  1. 0 1 1 0
  2. false true true Optional[NewYear] Correct Answer
  3. 0 1 0 Optional[NewYear]
  4. true true false NewYear

Community Votes

B
100%

100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

This question tests the return types of Stream match methods (boolean) versus find methods (Optional), alongside the common trap of misjudging predicate logic.

This page analyzes the output of Java Stream methods allMatch, anyMatch, noneMatch, and findFirst on a list of strings. It establishes that the correct result is false true true Optional[NewYear].

Choosing an option that outputs the raw string 'NewYear' instead of 'Optional[NewYear]', or incorrectly evaluating the noneMatch predicate as false.

Community Discussion (5 comments)

Uteman 👍 1
B is the correct answer
xplorerpj 👍 1 Selected: B
B is right answer
minhdev 👍 1
*Match returns boolean findFirst returns Optional Result: B is correct
james2033 👍 2 Selected: B
package q08; import java.util.List; public class Q08 { public static void main(String[] args) { List<String> specialDays = List.of("NewYear", "Valentines", "Spring", "Labour"); System.out.print(specialDays.stream().allMatch(s -> s.equals("Labour"))); System.out.print(" " + specialDays.stream().anyMatch(s -> s.equals("Labour"))); System.out.print(" " + specialDays.stream().noneMatch(s -> s.equals("Halloween"))); System.out.print(" " + specialDays.stream().findFirst()); } } // Result: // false true true Optional[NewYear]
Samps 👍 1 Selected: B
B is correct

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The allMatch method returns false because not all elements in the list equal "Labour". The anyMatch method returns true because at least one element ("Labour") matches the predicate. The noneMatch method returns true because no elements in the list match the evaluated predicate (e.g., a non-existent string). Finally, findFirst returns an Optional describing the first element, yielding Optional[NewYear]. Concatenating these results produces false true true Optional[NewYear].

Why the Other Options Are Wrong

Option A uses integers (0 and 1) instead of booleans, which is incorrect since Java's Match methods strictly return boolean. Option C incorrectly assumes noneMatch returns false for the evaluated predicate. Option D incorrectly returns true for allMatch and omits the Optional wrapper for the findFirst result, printing the raw string instead.

Community Comment Notes

Commenters confirm the behavior of the Stream methods, noting that "Match returns boolean findFirst returns Optional" as minhdev pointed out. James2033 provided a partial code snippet showing the list and the stream operations, which aligns perfectly with the false true true Optional[NewYear] output and validates option B.

Official Reference

Exam Strategy

Remember that Stream match methods (allMatch, anyMatch, noneMatch) always return a primitive boolean, while find methods like findFirst return an Optional. Trace the predicate logic carefully for each match method to avoid logical negation errors.

Related Analysis

← Back to 1Z0-829 Study Guide