Java Stream Match and FindFirst Results

1Z0-829 By islever Editorial Last updated

Short answer

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

Question

Given the code fragment: What is the result? - Exhibit: Java Stream Match and FindFirst Results
  1. 0 1 1 0
  2. false true true Optional[NewYear] Correct Answer
  3. 0 1 0 Optional[NewYear]
  4. true true false NewYear

Key Takeaways

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].

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

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

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html

This link opens the official vendor documentation in a new tab.

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.

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.

Discussion 5 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 5

  1. james2033 👍 2 Answered 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]
  2. minhdev 👍 1
    *Match returns boolean findFirst returns Optional Result: B is correct
  3. Uteman 👍 1
    B is the correct answer
  4. xplorerpj 👍 1 Answered B
    B is right answer
  5. Samps 👍 1 Answered B
    B is correct

Reader comments & corrections

No reader corrections 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.

← Back to 1Z0-829 Study Guide