ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
TreeMap<String, Integer> m = new TreeMap<>();
for (String k : new String[]{"9", "10", "100", "2"}) m.put(k, k.length());
System.out.print(m.keySet());- A. [2, 9, 10, 100]
- B. [9, 10, 100, 2]
- C. [10, 100, 2, 9]
- D. [100, 10, 9, 2]
Correct answer: C. [10, 100, 2, 9]
String keys are ordered lexicographically character by character, not numerically: "10" < "100" < "2" < "9" because '1' < '2' < '9'.
More OOP with C++ and Java questions
- What is printed? java String r = Collections.max(List.of("pear", "fig", "banana", "kiwi"), Comparator.comparing(String::length))…
- 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…