ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer c = 0, i = 16
while (i > 0)
for (each j from 1 to i)
c = c + 1
end for
i = i / 2
end while
Print c- A. 80
- B. 30
- C. 31
- D. 16
Correct answer: C. 31
The inner loop runs 16 + 8 + 4 + 2 + 1 = 31 times (i = 1 still runs once, then 1 / 2 = 0 ends the loop). This geometric pattern is O(n), not O(n log n). 16 × 5 = 80 is the trap.
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? (All variables are integers, so / is integer division.) Function f(Integer n) if (n <…
- How many lines does this pseudocode print for hanoi(5, 'A', 'B', 'C')? Function hanoi(Integer n, Char from, Char via, Char to) if (n == 0)…
- How many of the following pseudocode statements print 1? (/ is integer division; & is bitwise AND; ^ is bitwise XOR.) (i) Print (7 / 2) 2…
- What will be the output of the following pseudocode? Integer a = 12, b = 18, t while (b != 0) t = b b = a mod b a = t end while Print a
- What will be the output of the following pseudocode? Integer s = 0 for (i = 1 to 5) for (j = i to 5) s = s + 1 end for end for Print s
- What will be the output of the following pseudocode? Integer n = 1234, r = 0 while (n > 0) r = r 10 + (n mod 10) n = n / 10 // integer…