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)- A. 110 7
- B. 101 6
- C. 110 12
- 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.
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? Integer a[6] = {1, 2, 1, 3, 2, 1} Map cnt = empty Integer pairs = 0 for (each i from…
- What will be the output of the following pseudocode? Integer move = 0 Procedure hanoi(Integer n, Char a, Char b, Char c) if (n == 0)…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) j = 1 while (j < i) j = j 2 end…
- What is the time complexity of the following pseudocode on an array of n elements? Stack st = empty for (each i from 0 to n - 1) while (st…
- What will be the output of the following pseudocode? (c ? x : y evaluates x if c is true, otherwise y) Integer marks[6] = {92, 75, 60, 88…
- What will be the output of the following pseudocode? (continue in a do-while loop goes straight to the while test) Integer x = 1, c = 0 do…