Java OCA OCP Practice Question 2672

Question

What is the result of the following statements?

3:    ArrayDeque<String> greetings = new ArrayDeque<String>(); 
4:    greetings.push("hello"); 
5:    greetings.push("hi"); 
6:    greetings.push("ola"); 
7:    greetings.pop(); 
8:    greetings.peek(); 
9:    while (greetings.peek() != null) 
10:      System.out.print(greetings.pop()); 
A.  hello
B.  hellohi
C.  hellohiola
D.  hi
E.  hihello
F.  The code does not compile.
G.  An exception is thrown.


E.

Note

Since we call push() rather than offer(), we are treating the ArrayDeque as a LIFO (last-in, first-out) stack.

On line 7, we remove the last element added, which is "ola".

On line 8, we look at the new last element ("hi"), but don't remove it.

Lines 9 and 10, we remove each element in turn until none are left.

Note that we don't use an Iterator to loop through the ArrayDeque.

The order in which the elements are stored internally is not part of the API contract.




PreviousNext

Related