Suppose a driver program creates and uses a Stack and a Queu

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 BBB

Solution

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

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site