Java OCA OCP Practice Question 1731

Question

What is the output of the following?

public class Compete {
   public static void main(String[] args) {
      Stream<Integer> is = Stream.of(8, 6, 9);
      Comparator<Integer> c = (a, b) -> a - b;
      is.sort(c).forEach(System.out::print);
  }
}
  • A. 689
  • B. 986
  • C. The code does not compile
  • D. The code compiles but throws an exception at runtime.


C.

Note

There is not a stream pipeline method called sort().

There is one called sorted().

Since the code does not compile, Option C is correct.

If this was fixed, Option A would be correct since the Comparator sorts in ascending order.




PreviousNext

Related