ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
String r = Collections.max(List.of("pear", "fig", "banana", "kiwi"),
Comparator.comparing(String::length));
System.out.print(r);- A. banana
- B. pear
- C. fig
- D. kiwi
Correct answer: A. banana
Collections.max with a comparator returns the greatest element by that comparator, here the longest string. Without the comparator it would use alphabetical order and return pear.
More OOP with C++ and Java questions
- Which line fails to compile? java int[] a = {3, 1, 2}; Integer[] b = {3, 1, 2}; Arrays.sort(b, Collections.reverseOrder()); // L1…
- 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() + "]" +…