ProgrammingVery hardGoldman Sachs OA · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function F(Integer n)
if (n == 0)
return 1
end if
return n - M(F(n - 1))
End Function
Function M(Integer n)
if (n == 0)
return 0
end if
return n - F(M(n - 1))
End Function
Print F(6), M(6)- A. 4 3
- B. 4 4
- C. 3 4
- D. 3 3
Correct answer: B. 4 4
Build both tables upward: F = 1, 1, 2, 2, 3, 3, 4 and M = 0, 0, 1, 2, 2, 3, 4. For example F(6) = 6 − M(F(5)) = 6 − M(3) = 4 and M(6) = 6 − F(M(5)) = 6 − F(3) = 4. Output 4 4.
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? (Arrays are indexed from 0) Integer a[6] = {1, 2, 3, -2, 2, 1}, c = 0, s for (each i…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body) Integer c =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "110100" Integer v = 0, p = 1 for (each i…
- What will be the output of the following pseudocode? Boolean mark[2..30] = all false Integer ops = 0, j for (each p from 2 to 30) if (p p…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)…