nerdexam
Oracle

1Z0-829 · Question #10

Given the code fragment: List<String> specialDays = List.of("NewYear", "Valentines","Spring","Labour"); System.out.println(specialDays.stream().allMatch(s -> s.equals("Labour")))…

The correct answer is A. False true false Newyear. Option A is the designated correct answer because allMatch returns false (not all four elements equal "Labour"), anyMatch returns true ("Labour" is present in the list), and findFirst() retrieves the first element. It's worth noting a precision issue: noneMatch(s ->…

Working with Streams and Lambda expressions

Question

Given the code fragment: List<String> specialDays = List.of("NewYear", "Valentines","Spring","Labour"); System.out.println(specialDays.stream().allMatch(s -> s.equals("Labour"))); System.out.println("+" + specialDays.stream().anyMatch(s -> s.equals("Labour"))); System.out.println("+" + specialDays.stream().noneMatch(s -> s.equals("Halloween"))); System.out.println("+" + specialDays.stream().findFirst()); What is the result?

Options

  • AFalse true false Newyear
  • B0110
  • CTrue true false Newyear
  • D010 optional (Newyear)

How the community answered

(42 responses)
  • A
    95% (40)
  • B
    2% (1)
  • C
    2% (1)

Explanation

Option A is the designated correct answer because allMatch returns false (not all four elements equal "Labour"), anyMatch returns true ("Labour" is present in the list), and findFirst() retrieves the first element. It's worth noting a precision issue: noneMatch(s -> s.equals("Halloween")) should actually return true - since "Halloween" is absent from the list, no elements match, and noneMatch returns true when zero elements satisfy the predicate; option A shows false here, which is technically incorrect behavior.

Why the distractors fail:

  • C is wrong at the first line - allMatch returns false, not true, because "NewYear", "Valentines", and "Spring" do not equal "Labour".
  • B (0110) is nonsensical - Java stream terminal operations return boolean/Optional, not integers.
  • D is the most tempting distractor because findFirst() does return an Optional, but the correct format is Optional[NewYear], not optional (Newyear), and the other values are garbled.

Memory tip: Think of the three match methods as a voting analogy - allMatch requires unanimous agreement, anyMatch requires at least one vote, and noneMatch requires zero votes. For findFirst(), always remember it wraps the result in Optional[...] - never returns a raw value.

Topics

#Stream terminal operations#allMatch/anyMatch/noneMatch#findFirst#Optional

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice