ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
Map<String, Integer> m = new LinkedHashMap<>(16, 0.75f, true);
m.put("a", 1); m.put("b", 2); m.put("c", 3);
m.get("a");
m.put("b", 20);
System.out.print(m.keySet());- A. [a, b, c]
- B. [c, a, b]
- C. [b, a, c]
- D. [c, b, a]
Correct answer: B. [c, a, b]
The third constructor argument true selects ACCESS order: each get or put of an existing key moves it to the end. After get(a): b, c, a; after put(b): c, a, b. (This is the basis of an LRU cache.)
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new LinkedHashMap<>(); m.put("x", 1); m.put("y", 2); m.put("x", 3); System.out.print(m);
- What is printed? java TreeSet<Integer> s = new TreeSet<>(List.of(5, 1, 9, 3, 7)); System.out.print(s.headSet(5) + " " + s.tailSet(5) + " "…
- What is printed? java Set<List<Integer>> a = new HashSet<>(); a.add(List.of(1, 2)); a.add(new ArrayList<>(List.of(1, 2))); Set<int[]> b =…
- What is printed? java char[] c = {'h', 'i'}; System.out.print(c); System.out.print(" " + String.valueOf(c) + " " + c.length);
- What is printed? java // static methods of the main class: static void up(String s) { s = s.concat("X"); } static void add(StringBuilder…
- What is printed? java Object a = new ArrayList<String>(); List<Integer> b = new LinkedList<>(); List<Integer> c = new ArrayList<>()…