Skip to content

平成23年度 特別試験 ITパスポート試験 問58

This question's explanation and your answers so far.

問58
頻出度★☆☆

あるキューに要素 “33”, 要素 “27” 及び要素 “12” の三つがこの順序で格納されている。このキューに要素 “45” を追加した後に要素を二つ取り出す。2 番目に取り出される要素はどれか。

出典 平成23年度 特別試験 ITパスポート試験 問58
解説

問 58

  • Incorrectア

    Counting from the front, 12 is the third element, so it only comes out on the third removal. Two removals bring out only 33 and 27, so this is wrong.

  • イ

    正解 A queue is a FIFO (first-in, first-out) data structure: elements are always added at the back (enqueue) and always taken out from the front (dequeue). In this question the elements are stored in the order 33, 27, 12, so the front is 33, the next is 27, and after that comes 12. Adding 45 attaches it at the back, so the queue from the front is 33, 27, 12, 45. Taking two elements out of this state, the first one out is 33 at the front and the second one out is the next element, 27. Therefore the second element taken out is 27, and イ is the correct answer.

  • Incorrectウ

    33 is the first element stored and sits at the front, so under FIFO it is the first element taken out. The question asks for the second one, so this is wrong.

  • Incorrectエ

    45 is the element added last and attaches to the back of the queue. Taking it out first is the behavior of a stack (LIFO, last-in first-out); in a queue it would come out on the fourth (last) removal, so this is wrong.

ここがポイント
  • A queue is FIFO (first-in, first-out): add at the back (enqueue) and take out from the front (dequeue). The order elements come out is the same as the order they went in.
  • Its counterpart, the stack, is LIFO (last-in, first-out), where the item put in last comes out first. Not mixing up the difference in removal order between the two is the key point of this question.
用語

Your answers