Java SE 8 Exam 1Z0-809: Question Analysis

Java SE 8 Exam 1Z0-809: Question Analysis

Question

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

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

C.

The given code fragment performs the following operations:

  1. Creates a List of Strings using the Arrays.asList() method and assigns it to nL.
  2. Creates a lambda expression funVal of type Function<String, String> which takes a String argument and returns a new String which concatenates "Hello : " and the input String.
  3. Calls the stream() method on the nL list to get a Stream of its elements.
  4. Calls the map() method on the Stream with the funVal lambda expression as an argument. This transforms each String element of the Stream into a new String by applying the funVal lambda expression to it.
  5. Calls the peek() method on the Stream with a method reference to System.out::print as an argument. This prints each element of the Stream to the console.

The result of the code fragment is that each element of the nL list is transformed into a new String with the funVal lambda expression and printed to the console using the peek() method. The output is:

Hello : Jim Hello : John Hello : Jeff

Therefore, the correct answer is A.