ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Deque<Integer> d = new ArrayDeque<>(); d.push(1); d.offer(2); d.push(3); d.offerFirst(4); d.add(5); System.out.print(d.pop() + " " + d.pollLast() + " " + d);
- A. 5 4 [3, 1, 2]
- B. 4 5 [2, 1, 3]
- C. 1 5 [4, 3, 2]
- D. 4 5 [3, 1, 2]
Correct answer: D. 4 5 [3, 1, 2]
push/offerFirst add at the head, offer/add at the tail: the deque becomes [4, 3, 1, 2, 5]. pop removes the head (4), pollLast the tail (5), and a deque prints head to tail.
More OOP with C++ and Java questions
- What is printed? java Stack<Integer> st = new Stack<>(); Deque<Integer> dq = new ArrayDeque<>(); for (int i = 1; i <= 3; i++) {…
- What is printed? java TreeMap<Integer, String> m = new TreeMap<>(); for (int k : new int[]{10, 20, 30, 40}) m.put(k, "v" + k)…
- What is printed? java Map<String, Integer> h = new HashMap<>(); h.put(null, 1); h.put(null, 2); System.out.print(h.size() + " " +…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); int x = m.getOrDefault("b", 0) + m.get("a"); int y =…
- What is printed? java Map<Character, Integer> f = new TreeMap<>(); for (char c : "banana".toCharArray()) f.merge(c, 1, Integer::sum)…
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.putIfAbsent("k", 1) + " ")…