ProgrammingHardCapgemini · Infosys pattern
How many lines does this pseudocode print for hanoi(5, 'A', 'B', 'C')?
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)
return
end if
hanoi(n - 1, from, to, via)
Print "Move", n, from, to
hanoi(n - 1, via, from, to)
End Function- A. 31
- B. 32
- C. 25
- D. 63
Correct answer: A. 31
Moves follow M(n) = 2·M(n-1) + 1 with M(0) = 0, so M(n) = 2ⁿ - 1 = 31. 2⁵ = 32 counts one too many, and 63 is M(6).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- What will be the output of the following pseudocode? Integer x = 5, y = 9 x = x ^ y // ^ is bitwise XOR y = x ^ y x = x ^ y Print x, y, (5…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n <= 0) return 0 return (n mod 10) + f(n / 10) //…