nerdexam
Oracle

1Z0-809 · Question #89

Given the code fragment: List<String> nl = Arrays.asList ("Jim", "John", "Jeff"); Function<String, String> funVal = s -> "Hello : ".concat(s); nL.stream() .map (funVal) .peek (System.out::print)…

The correct answer is C. The program prints nothing. Option C is correct because Java streams are lazy - intermediate operations like .map() and .peek() only describe the pipeline; they never execute without a terminal operation (e.g., .forEach(), .collect(), .count()). Since this code ends with .peek() and never calls a terminal…

Question

Given the code fragment: List<String> nl = Arrays.asList ("Jim", "John", "Jeff"); Function<String, String> funVal = s -> "Hello : ".concat(s); nL.stream() .map (funVal) .peek (System.out::print); What is the result?

Options

  • AHello : Jim Hello : John Hello : Jeff
  • BJim John Jeff
  • CThe program prints nothing.
  • DA compilation error occurs.

How the community answered

(65 responses)
  • A
    6% (4)
  • B
    17% (11)
  • C
    74% (48)
  • D
    3% (2)

Explanation

Option C is correct because Java streams are lazy - intermediate operations like .map() and .peek() only describe the pipeline; they never execute without a terminal operation (e.g., .forEach(), .collect(), .count()). Since this code ends with .peek() and never calls a terminal operation, the stream is constructed but never triggered, producing no output and no side effects.

Why the distractors are wrong:

  • A is wrong because even if the stream ran, the output would require a terminal operation to pull data through the pipeline - .peek() alone cannot do this.
  • B is wrong for the same reason, and also because funVal prepends "Hello : ", so raw names would never appear even if it did run.
  • D is tempting because nl is declared but nL (capital L) is used - a real case-sensitivity error in Java - but this question treats it as the same variable to focus on the lazy evaluation concept.

Memory tip: Think of a stream pipeline as a recipe written on paper - writing it does nothing. You need a terminal operation as the "cook" to actually execute it. No terminal = no cooking = no output.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice