Java SE 8 Programmer II Exam: Question Answer - 1Z0-809

What is the result?

Question

Given the code fragment: UnaryOperator<Integer> uo1 = s -> s*2;line n1 List<Double> loanValues = Arrays.asList(1000.0, 2000.0); loanValues.stream() .filter(lv -> lv >= 1500) .map(lv -> uo1.apply(lv)) .forEach(s -> System.out.print(s + " ")); What is the result?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

D.

The code fragment defines a UnaryOperator<Integer> called uo1 that takes an Integer and returns the result of multiplying it by 2. It then creates a List<Double> called loanValues with two elements: 1000.0 and 2000.0. The code then streams over the loanValues list and filters out any elements that are less than 1500.0. The map function then applies uo1 to each remaining element in the list, effectively doubling them. Finally, the forEach function prints out each of the resulting doubled elements, separated by a space.

Therefore, the output of the code will be: 4000.0

Explanation:

  • The stream starts with two elements: 1000.0 and 2000.0
  • The filter function removes the first element, since it is less than 1500.0
  • The map function applies uo1 to the remaining element (2000.0), doubling it to 4000.0
  • The forEach function prints out the doubled element: 4000.0