ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
Queue<Integer> q = new ArrayDeque<>(); q.add(1); q.add(2); q.offer(3); q.poll(); System.out.print(q.peek() + " " + q.size());
- A. 2 2
- B. 3 2
- C. 1 3
- D. 2 3
Correct answer: A. 2 2
As a Queue, ArrayDeque is FIFO: add/offer append at the tail, poll removes the head (1), peek shows the new head (2) without removing it.
More OOP with C++ and Java questions
- What is printed? java System.out.print(Stream.of(1, 2, 3).reduce(10, Integer::sum));
- What is printed? java long n = Stream.of(List.of(1, 2), List.of(2, 3)) .flatMap(List::stream).distinct().count(); System.out.print(n);
- What is printed? java Map<Boolean, List<Integer>> m = Stream.of(2, 4) .collect(Collectors.partitioningBy(x -> x > 5))…
- What happens? java interface Show { String toString(); } class C implements Show { } // in main: System.out.print(new C() instanceof Show);
- What is printed? java System.out.print((Double.NaN == Double.NaN) + " " + Double.valueOf(Double.NaN).equals(Double.NaN) + " " + (0.0 ==…
- A class overrides equals correctly, but its hashCode() returns 42 for every object. What happens when many such objects are stored in a…