ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi"));
l.sort(Comparator.comparing(String::length).thenComparing(Comparator.reverseOrder()));
System.out.print(l);- A. [fig, kiwi, pear, apple]
- B. [fig, pear, kiwi, apple]
- C. [apple, pear, kiwi, fig]
- D. [fig, pear, apple, kiwi]
Correct answer: B. [fig, pear, kiwi, apple]
The primary key is length (3, 4, 4, 5). The tie between the 4-letter words is broken by reverse natural order, so pear comes before kiwi: [fig, pear, kiwi, apple].
More OOP with C++ and Java questions
- Why does this class fail to compile? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }
- What is printed? java record Pt(int x, int y) { } // in main: Set<Pt> s = new HashSet<>(); s.add(new Pt(1, 2)); s.add(new Pt(1, 2))…
- Which line fails to compile? java class Animal { Animal make() { return new Animal(); } } class Dog extends Animal { Dog make() { return…
- What is printed? java PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder()); pq.addAll(List.of(3, 1, 4, 1, 5))…
- A Printer interface declares print(), scan(), fax() and staple(). A cheap printer class must implement all four and throws…
- What is printed? java String a = "Hi"; String b = new String("Hi"); String c = b.intern(); StringBuilder d = new StringBuilder("Hi")…