Java OCA OCP Practice Question 2156

Question

What is the output of the following?.

public class Main { 
   public static void main(String[] args) { 
      Stream<Integer> is = Stream.of(8, 6, 9); 
      Comparator<Integer> c = (a, b) -> b - a;  // r1 
      is.sorted(c).forEach(System.out::print);  // r2 
  } 
} 
  • A. 689
  • B. 986
  • C. The code does not compile because of line r1.
  • D. The code does not compile because of line r2.
  • E. The code does not compile due to another line.
  • F. The code compiles but throws an exception at runtime.


B.

Note

First, remember that you are supposed to assume missing imports are present so you can act as if java.util and java.util.stream are imported.

This code does compile.

Line r1 is a valid lambda definition of a Comparator.

Line r2 is valid code to sort a stream in a descending order and print the values.

Therefore, Option B is correct.




PreviousNext

Related