Java SE 8 Programmer II Exam: Code Fragment Result

Code Fragment Result

Question

Given the code fragment: List<Integer> codes = Arrays.asList (10, 20); UnaryOperator<Double> uo = s -> s +10.0; codes.replaceAll(uo); codes.forEach(c -> System.out.println(c)); 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. Create a list of integers with values 10 and 20 using the Arrays.asList method and store it in the codes variable.
php
List<Integer> codes = Arrays.asList(10, 20);
  1. Create a UnaryOperator instance uo that adds 10.0 to a given Double value.
kotlin
UnaryOperator<Double> uo = s -> s + 10.0;
  1. Use the replaceAll method of the List interface to replace all the elements of the codes list with the result of applying the uo operator to each element of the list.
scss
codes.replaceAll(uo);

After this step, the codes list contains the elements [20.0, 30.0], because uo.apply(10) returns 20.0 and uo.apply(20) returns 30.0.

  1. Use the forEach method of the List interface to print each element of the codes list.
r
codes.forEach(c -> System.out.println(c));

This will print:

20.0 30.0

Therefore, the correct answer is option A, which is "20.0 30.0". There are no compilation errors or runtime exceptions in the given code fragment.