ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.)
What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.)
Integer calls = 0
Function f(Integer n)
calls = calls + 1
if (n == 0)
return 1
end if
return f(n / 2) + f(n / 3)
End Function
Print f(12), calls- A. 12 12
- B. 7 13
- C. 13 23
- D. 12 23
Correct answer: D. 12 23
Build up: f(0) = 1, f(1) = f(0) + f(0) = 2, f(2) = f(1) + f(0) = 3, f(3) = f(1) + f(1) = 4, f(4) = f(2) + f(1) = 5, f(6) = f(3) + f(2) = 7, f(12) = f(6) + f(4) = 12. Calls follow C(0) = 1 and C(n) = 1 + C(n/2) + C(n/3): C(1) = 3, C(2) = 5, C(3) = 7, C(4) = 9, C(6) = 13, C(12) = 23. '7 13' are the values for f(6).
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? (^ is bitwise XOR and >> is a right shift.) Integer g = 59, n = 0 while (g > 0) n = n…
- How many times does the body of this loop run? // c is an 8-bit unsigned integer: it holds 0 to 255 and arithmetic wraps modulo 256…
- What will be the output of the following pseudocode? (/ is integer division.) Integer a = 7, b = 3 a = a b - a / b Print a
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 20) if (i mod 3 == 0 or i mod 5 == 0) c = c + 1 end if…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 2405, c = 0, s = 0 while (n > 0) s = s + n mod…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 4) for (j = 1 to 4) if (j > i) break end if c = c + j end…