ProgrammingHardAmazon OA · Infosys SP

What will be the output of the following pseudocode? (+ joins strings)

What will be the output of the following pseudocode? (+ joins strings)
Queue q = {"1"}
Integer k = 0
String s
while (k < 6)
    s = dequeue(q)
    k = k + 1
    enqueue(q, s + "0")
    enqueue(q, s + "1")
end while
Print s, size(q)
  1. A. 110 7
  2. B. 101 6
  3. C. 110 12
  4. D. 111 7

Correct answer: A. 110 7

The queue produces binary numbers in increasing order: 1, 10, 11, 100, 101, 110. The 6th dequeued is 110. Each dequeue adds 2 and removes 1, so the queue holds 1 + 6 = 7 items: 110 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic