ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
List<String> l = Arrays.asList("b", null, "a");
l.sort(Comparator.nullsFirst(Comparator.naturalOrder()));
System.out.print(l);- A. [a, b, null]
- B. A NullPointerException is thrown
- C. [null, a, b]
- D. [b, null, a]
Correct answer: C. [null, a, b]
nullsFirst wraps a comparator so that null is treated as smaller than every non-null value; the non-null elements use natural order. Plain natural order would throw NullPointerException.
More OOP with C++ and Java questions
- What is printed? java // static method of the main class: static String make() { System.out.print("M"); return "y"; } // in main…
- What is printed? java Stream<Integer> s = Stream.of(1, 2, 3); System.out.print(s.count()); System.out.print(s.count());
- What happens when this method is compiled (Java 7 or later)? java static void f() throws java.io.IOException { try { if (Math.random() <…
- What is printed? java try { System.out.print("T"); System.exit(0); } finally { System.out.print("F"); }
- What is printed? java class N { public Object clone() throws CloneNotSupportedException { return super.clone(); } } // in main: try { new…
- What is printed? java System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " " + (Set.of(1, 2).hashCode() ==…