ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
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 Function
Print f(19)- A. 10011
- B. 11001
- C. 19
- D. 1011
Correct answer: A. 10011
The function writes n's binary digits as a decimal number: the recursion handles the higher bits first, then appends n mod 2. 19 = 16 + 2 + 1 = 10011₂. Building the digits in the loop order without recursion would reverse them (11001).
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? 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]…
- What will be the output of the following pseudocode? Integer n = 5, t = 0 for (i = 1 to n) if (i mod 2 == 1) t = t + i i else t = t - i…
- What will be the output of the following pseudocode? Integer a = 1, b = 1, t for (k = 1 to 5) t = a + b a = b b = t if (b mod 2 == 0)…
- What will be the output of the following pseudocode? Integer Function f(Integer n, Integer acc) if (n == 0) return acc return f(n - 1…