ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
Procedure rec(Integer k)
c = c + 1
if (k <= 1) return
rec(k / 2)
rec(k / 2)
End Procedure
rec(6)
Print c- A. 5
- B. 7
- C. 9
- D. 15
Correct answer: B. 7
rec(6) calls rec(3) twice; each rec(3) calls rec(1) twice. Calls: 1 + 2·(1 + 2) = 7. The recursion tree is binary with depth ⌊log₂ k⌋, so roughly 2k calls – T(k) = 2T(k/2) + 1 = O(k).
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? // 32-bit signed integers with two's-complement wrap-around Integer x = 2147483647…
- What will be the output of the following pseudocode? Integer a = 13, b = 10 // 1101 and 1010 Print a & b, a | b, a ^ b, ~a // 32-bit two's…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 0) return 0 return f(n / 2) 10 + (n mod 2) End…
- What will be the output of the following pseudocode? Stack st = empty for (each x in {3, 1, 4, 1, 5, 9, 2, 6}) while (st not empty AND…
- What will be the output of the following pseudocode? Integer a[] = {5, 1, 4, 2, 3}, c = 0 for (i = 0 to 4) for (j = i + 1 to 4) if (a[i] >…
- What will be the output of the following pseudocode? String s = "PROGRAM", r = "" for (i = length(s) - 1 down to 0 step 2) r = r + s[i]…