1 1 vote Consider the following Python code fragment:stack = [] while len(q) > 0: stack.append(q.pop(0)) while len(stack) > 0: q.append(stack.pop())Here, $\texttt{q.pop(0)}$ removes the front element of the queue, and $\texttt{q.append(x)}$ inserts $\texttt{x}$ at the rear of the queue.What does this code fragment do to the queue $\texttt{q}$?It leaves the queue unchanged.It reverses the items in the queue.It removes all items from the queue permanently.It sorts the queue in increasing order. Algorithms goclasses goclasses-da-dpp goclasses-da-dpp-day-219 python-&-dsa goclasses-python-&-dsa-practice-questions stack + – GO Classes 139 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
1 1 vote The first loop removes items from the front of the queue and pushes them onto the stack.Suppose the queue initially is:$\texttt{q = ["A", "B", "C"]}$After the first loop:$\texttt{stack = ["A", "B", "C"]}$$\texttt{q = []}$Now the second loop pops from the stack. Since stack follows LIFO order, the popped order is:$\texttt{C, B, A}$These are appended back to the queue.So the final queue becomes:$\texttt{q = ["C", "B", "A"]}$Therefore, the queue is reversed.Correct Option: B GO Classes answered Jul 9 GO Classes comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Answer: B Meticulous_March answered Jul 10 Meticulous_March comment Share Follow 0 reply Please log in or register to add a comment.