ProgrammingVery hardInfosys · Capgemini pattern
What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?
What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?
enqueue 98 enqueue 18 enqueue 3 enqueue 53 enqueue 58 enqueue 39 enqueue 40 dequeue enqueue 61 enqueue 88
- A. 18, 3, 53, 58, 39, 40, 61
- B. 18, 3, 53, 58, 39, 40, 61, 88
- C. 88, 61, 40, 39, 58, 53, 3, 18
- D. 98, 18, 3, 53, 58, 39, 61, 88
Correct answer: B. 18, 3, 53, 58, 39, 40, 61, 88
A queue is FIFO: dequeue always removes the front (oldest) element. Tracing the sequence gives front-to-back [18, 3, 53, 58, 39, 40, 61, 88]. Treating it like a stack (removing the most recent instead) gives the trap [98, 18, 3, 53, 58, 39, 61, 88]; reading it back-to-front gives [88, 61, 40, 39, 58, 53, 3, 18].
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 25, y = 20 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 2. What does each of the 5 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…