ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer Function h(Integer n)
if (n <= 0) return 1
if (n mod 2 == 1) return h(n - 1) + h(n - 2)
return 2 * h(n - 1)
End Function
Print h(6)- A. 32
- B. 64
- C. 24
- D. 36
Correct answer: D. 36
h(0) = h(−1) = 1; h(1) = 1 + 1 = 2; h(2) = 4; h(3) = 4 + 2 = 6; h(4) = 12; h(5) = 12 + 6 = 18; h(6) = 36. Note h(−1) also hits the base case.
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 v = 0 for (i = 1 to 5) v = (v << 1) | (i & 1) end for Print v
- What will be the output of the following pseudocode? Integer a[] = {10, 20, 30, 40} Integer i = 0, s = 0 while (i < 4) s = s + a[i] i = i…
- What will be the output of the following pseudocode? (memo starts with every entry equal to −1; calls counts every call, including the…
- What will be the output of the following pseudocode? (calls counts every call, including the first.) Integer calls = 0 Function g(Integer…
- What will be the output of the following pseudocode? (& is bitwise AND and >> is a right shift.) Integer c = 0 for (each x from 1 to 200)…
- What will be the output of the following pseudocode? (Integers are in two's complement, so −i flips the bits of i and adds 1; & is bitwise…