Java OCA OCP Practice Question 1693

Question

What is the result of the following?

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Main {
   public static void main(String[] args) {
      List<Double> list = new ArrayList<>();
      list.add(5.4);/*from  w w  w  . ja v a2  s . c  om*/
      list.add(1.2);
      Optional<Double> opt = list.stream().sorted().findFirst();
      System.out.println(opt.get() + " " + list.get(0));

   }
}
  • A. 1.2 1.2
  • B. 1.2 5.4
  • C. 5.4 5.4
  • D. None of the above


B.

Note

This code builds a list with two elements.

It then uses that list as a source for the stream, sorts the stream as it goes by, and grabs the first sorted element.

This does not change the original list.

The first element in the sorted stream is 1.2, but the first element of list remains as 5.4.

Option B is correct.




PreviousNext

Related