Suppose a driver program creates and uses a Stack and a Queue like this. What wi
ID: 3779677 • Letter: S
Question
Suppose a driver program creates and uses a Stack and a Queue like this. What will print?//: Queue's .insert is the same as .enqueue and remove is the same as .dequeue Stack myStack = new Stack0; Queue myQueue = new Queue0; String result; myStack.push("AAA"); myStack.push("BBB"); myQueue.insertO'CCC"); myQueue.insert("DDD"); myStack.push(myQueue.peek()); myQueue.insert(myStack.pop()); result = myStack.peekO + * ' + myQueue.peekO; System.out.println(result); BBB CCC An exception will be thrown AAA CCC CCC BBBExplanation / Answer
Answer: BBB CCC
First we inserted "AAA", "BBB" strings to stack and "CCC" and "DDD" to queue.
myStack.push(myQueue.peek())
we inserted first in queue to stack. so stack elements now "CCC", "BBB", "AAA"
myQueue.push(myStack.pop())
we removed first element from stack and added to queue
So queue elements now "CCC", "DDD", "CCC"
stack elements now "BBB", "AAA"
in result object, we are holding first elements of stack and queue so
it will return
BBB CCC
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.