ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Set<String> s = new LinkedHashSet<>(List.of("a", "b", "c"));
s.remove("a");
s.add("a");
s.add("b");
System.out.print(s);- A. [a, b, c]
- B. [b, c, a, b]
- C. [c, a, b]
- D. [b, c, a]
Correct answer: D. [b, c, a]
LinkedHashSet keeps insertion order. After removal, "a" is a new insertion at the end; adding "b" again changes nothing because it is already present.
More OOP with C++ and Java questions
- What is printed? java TreeMap<String, Integer> m = new TreeMap<>(); for (String k : new String[]{"9", "10", "100", "2"}) m.put(k…
- 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…