Assume you have a Stack class that defines the push pop and peek operations as w
ID: 3574563 • Letter: A
Question
Assume you have a Stack class that defines the push pop and peek operations as we discussed in lecture, What is displayed on the screen from running the following code? Stack foo = new stack (); foo. Push ("# loth"); foo. Push ("bear") system. out. Printin (fpp. Pop ()); foo. Peek (); system. out. Printin (foo. Peek()); foo. Push ("owl"); system. out. Printin (foo. Peek()); foo. Pop(); foo. Push ("Harambe"); system. out. Printin (foo. peek()); Consider the following ArrayList class: public class ArrayList {private E[] data = (E []) (new Object [5]); private int size = 0;//updated whenever elements are added/removed} Write the method add (E mew Item) for this class. This method should add the specified newItem to the end the list. Be sure to reallocate the data array in necessary!?Explanation / Answer
4) The output will be:
bear
sloth
owl
Harambe
Let us consider the state of the stack:
First we push "sloth"
then we push "bear"
Now the top will be "bear" so it will be printed. It is also removed from the stack.
Now peek will return the top of the stack. Which is "sloth". So it is printed.
Now "owl" is pushed. It will at the top of the stack.
Now peek will return the top of the stack. Which is "owl". So it is printed.
When we execute pop "owl" is removed. "Sloth" is at the top.
We push "Harambe".
Now peek will return the top of the stack. Which is "Harambe". So it is printed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.