nerdexam
Oracle

1Z0-809 · Question #66

Given the code fragment: ``java List<String> nl = Arrays.asList("Jim", "John", "Jeff"); Function<String, String> funVal = s -> "Hello : ".concat(s); nl.stream() .map(funVal) .forEach(s ->…

The correct answer is A. Hello : Jim Hello : John Hello : Jeff. Option A is correct because the code streams over the list, applies funVal via .map() which prepends "Hello : " to each name, then prints each result with .forEach() - producing Hello : JimHello : JohnHello : Jeff (no spaces between outputs since print not println is used, but…

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)
 .forEach(s -> System.out.print (s));
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

(28 responses)
  • A
    82% (23)
  • B
    11% (3)
  • C
    4% (1)
  • D
    4% (1)

Explanation

Option A is correct because the code streams over the list, applies funVal via .map() which prepends "Hello : " to each name, then prints each result with .forEach() - producing Hello : JimHello : JohnHello : Jeff (no spaces between outputs since print not println is used, but the choice shows them space-separated for readability).

Why the distractors are wrong:

  • B is wrong because .map(funVal) transforms each element - the original names are never printed.
  • C is wrong because the stream pipeline is valid and fully terminal (.forEach triggers evaluation); streams are lazy but do execute when a terminal operation is present.
  • D is wrong because the code compiles cleanly - Function<String, String> is the correct functional interface for a lambda that takes and returns a String, and .map() accepts it.

Memory tip: Think of .map() as a transformer - it replaces each element with whatever the function returns. If you see a Function<T, R> lambda plugged into .map(), ask "what does the function return?" - that's what ends up in the stream, not the original value.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice