ProgrammingHardTCS NQT Advanced · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer calls = 0
Function f(Integer a, Integer b)
calls = calls + 1
if (b == 0)
return a
end if
return f(b, a mod b)
End Function
Integer g = f(21, 34)
Print g, calls- A. 1 8
- B. 1 10
- C. 1 9
- D. 7 9
Correct answer: C. 1 9
When a < b the first call just swaps them: f(21, 34) → f(34, 21). Then 34, 21, 13, 8, 5, 3, 2, 1, 0 follow the Fibonacci numbers down. Counting every call from f(21, 34) to f(1, 0) gives 9: output 1 9.
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 g[5][5] = {{1,1,0,0,0}, {1,0,0,1,1}, {0,0,1,0,0}, {1,0,1,1,0}, {1,0,0,0,1}}…
- What will be the output of the following pseudocode? Integer m[4][4], s = 0 for (each i from 0 to 3) for (each j from 0 to 3) m[i][j] = 4…
- What will be the output of the following pseudocode? Boolean comp[0..30] = all false Integer s = 0 for (each i from 2 to 30) if (not…
- What will be the output of the following pseudocode? Function isPrime(Integer n) if (n < 2) return false for (each d from 2 to n - 1) if…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function phi(Integer n)…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 99) if (i mod 2 == 1) s = s + i else s = s - i…