ProgrammingHardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
int[] a = {3, 1, 2};
Integer[] b = {3, 1, 2};
Arrays.sort(b, Collections.reverseOrder());   // L1
Arrays.sort(a, Collections.reverseOrder());   // L2
  1. A. L2 only
  2. B. L1 only
  3. C. Both
  4. D. Neither

Correct answer: A. L2 only

The Comparator overloads of Arrays.sort exist only for object arrays (T[]); primitives cannot be passed to a Comparator. An int[] must be sorted ascending and reversed, or boxed first.

More OOP with C++ and Java questions

Exams that ask this topic