Return to site

JAVA CERTIFICATION QUESTION: Deque poll and peek

· java,ocp

Given:

What is the output of the given code fragment?

  • A. 1 1 1
  • B. 1 1 2
  • C. 1 2 1
  • D. 1 2 2
  • E. 2 1 1
  • F. 2 1 2
  • G. 2 2 1
  • H. 2 2 2

I. An exception is thrown

#java #certificationquestion #ocp

The offer method inserts the specified element at the tail of the deque. So we insert 1 then 2 at the tail of that deque.

peek() retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

poll() retrieves and removes the head of this queue, or returns null if this queue is empty.

So we read but not remove 1.

Then we read and remove 1.

FInally we read but not remove 2.

So the result is 1 1 2.

·ꓭ sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥʇ :uoᴉsnʅɔuoꓛ