ProgrammingHardInfosys · Accenture pattern
What will be the output of the following pseudocode? (calls counts every call, including the first; mod is the remainder.)
What will be the output of the following pseudocode? (calls counts every call, including the first; mod is the remainder.)
Integer calls = 0
Function g(Integer a, Integer b)
calls = calls + 1
if (b == 0)
return a
end if
return g(b, a mod b)
End Function
Print g(89, 55), calls- A. 1 9
- B. 1 10
- C. 1 11
- D. 34 10
Correct answer: B. 1 10
Consecutive Fibonacci numbers are the worst case for Euclid: (89, 55) → (55, 34) → (34, 21) → (21, 13) → (13, 8) → (8, 5) → (5, 3) → (3, 2) → (2, 1) → (1, 0). That is 10 calls, the last one returning gcd = 1. Forgetting the final call with b = 0 gives 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? (& is bitwise AND.) Integer total = 0 for (i = 1 to 15) Integer n = i while (n != 0)…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 0; i < 10; i = i + 1) if (i mod 3 == 0) i = i + 2 end if c…
- What will be the output of the following pseudocode? (&& binds more tightly than ||; ! is NOT.) Integer a = 5, b = 10, c = 15 if (a > b ||…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 6, steps = 0, peak = n while (n != 1) if (n mod…
- What will be the output of the following pseudocode? (reverse(x) returns x with its decimal digits reversed.) Integer n = 87, k = 0 while…
- What will be the output of the following pseudocode? Procedure fun(Integer n) if (n <= 0) return end if fun(n - 2) Print n fun(n - 1) End…