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- A. L2 only
- B. L1 only
- C. Both
- 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
- Which line fails to compile? java class P { void hi() { } } class C extends P { void a() { super.hi(); } // L1 static void b() {…
- What is printed? java Object[] objs = {1, "a", 2.0, 'c'}; for (Object o : objs) System.out.print(o.getClass().getSimpleName());
- What is printed? java class K { static int made = 0; K(int v) { made++; if (v < 0) throw new IllegalArgumentException(); } } // in main…
- What is printed? java interface Named { default String n() { return getClass().getSimpleName(); } } class Dog implements Named { } // in…
- What is printed? java Runnable r = new Runnable() { public void run() { } }; System.out.print("[" + r.getClass().getSimpleName() + "]" +…
- Which line fails to compile? java class P { final void f(int x) { } } class C extends P { void f(long x) { } // L1 void f(int y) { } // L2 }