Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output of the following segment of code? ArrayUnbndQueue<Integer> q1

ID: 675386 • Letter: W

Question

What is the output of the following segment of code?

ArrayUnbndQueue<Integer> q1 = new ArrayUnbndQueue<Integer>(); ArrayUnbndQueue<Integer> q2 = new ArrayUnbndQueue<Integer>(); ArrayUnbndQueue<Integer> q3 = new ArrayUnbndQueue<Integer>(); for(inti=0;i< 10;i++)

q1.enqueue(i); while (!q1.isEmpty())

q2.enqueue(q1.dequeue() * 2); for(inti=5;i< 12;++i)

q1.enqueue(i);
while (q1.dequeue() != 7)

q3.enqueue(2 + q1.dequeue()); while (!q1.isEmpty())

System.out.print(q1.dequeue() + " "); System.out.println();
while (!q2.isEmpty())

System.out.print(q2.dequeue() + " "); System.out.println();
while (!q3.isEmpty())

System.out.print(q3.dequeue() + " ");

Explanation / Answer

ArrayUnbndQueue<Integer> q1 = new ArrayUnbndQueue<Integer>(); //Initializing a Queue q1.
ArrayUnbndQueue<Integer> q2 = new ArrayUnbndQueue<Integer>(); //Initializing a Queue q2.
ArrayUnbndQueue<Integer> q3 = new ArrayUnbndQueue<Integer>(); //Initializing a Queue q3.
//Enqueue will add an element to the queue.
//Dequeue will delete an element from the queue.
for(int i=0;i< 10;i++) //Add 9 elements to the queue q1.
q1.enqueue(i);
//q1 will have elements: 9->8->7->6->5->4->3->2->1->0
//q2 and q3 are empty.
while (!q1.isEmpty()) //While q1 is not empty, which means you keep deleting one by one elements from the queue q1.
q2.enqueue(q1.dequeue() * 2); //Every deleted element from q1 will be doubled and enqueued to the queue q2.
//q2 will have elements: 18->16->14->12->10->8->6->4->2->0
//q1 and q3 are empty.
for(inti=5;i< 12;++i) //This loop runs for 7 times starting from 5 to 11.
q1.enqueue(i); //Add 7 elements to the queue q1.
//q1 will have elements: 11->10->9->8->7->6->5
//q2 will have elements: 18->16->14->12->10->8->6->4->2->0
//q3 is empty.
while (q1.dequeue() != 7) //Keep deleting elements from the queue q1, till the deleted element is not 7.
q3.enqueue(2 + q1.dequeue()); //Delete an element from queue q1, add 2 to that value, and enqueue that element to q3.
//q1 will have elements: 6->5
//q2 will have elements: 18->16->14->12->10->8->6->4->2->0
//q3 will have elements: 10->12
//Note that in this logic, you are dequeueing twice every time you run the loop.
while (!q1.isEmpty())//As long as q1 is not empty.
System.out.print(q1.dequeue() + " "); //Delete the elements from the queue q1 and print them to the screen.
System.out.println(); //Move to new line.
while (!q2.isEmpty())//As long as q2 is not empty.
System.out.print(q2.dequeue() + " "); //Delete the elements from the queue q2 and print them to the screen.
System.out.println();//Move to new line.
while (!q3.isEmpty())//As long as q3 is not empty.
System.out.print(q3.dequeue() + " "); //Delete the elements from the queue q3 and print them to the screen.
//At this point all the queues will become empty.

And the screen will show the values:

6 5
18 16 14 12 10 8 6 4 2 0
10 12

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote