ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Stack<Integer> st = new Stack<>();
Deque<Integer> dq = new ArrayDeque<>();
for (int i = 1; i <= 3; i++) { st.push(i); dq.push(i); }
System.out.print(st + " " + dq);- A. [3, 2, 1] [3, 2, 1]
- B. [1, 2, 3] [3, 2, 1]
- C. [1, 2, 3] [1, 2, 3]
- D. [3, 2, 1] [1, 2, 3]
Correct answer: B. [1, 2, 3] [3, 2, 1]
Stack extends Vector and push appends at the END, so it prints bottom to top. ArrayDeque.push inserts at the HEAD and a deque prints head first, so the top comes first.
More OOP with C++ and Java questions
- 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) + " ")…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6)); l.subList(1, 4).clear(); System.out.print(l);