ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer Function f(Integer x, Integer y)
if (x > y) return f(x - y, y)
if (x < y) return f(x, y - x)
return x
End Function
Print f(48, 18)- A. 6
- B. 18
- C. 3
- D. 12
Correct answer: A. 6
Subtraction form of Euclid: (48, 18) → (30, 18) → (12, 18) → (12, 6) → (6, 6) → 6 = gcd. It is correct but can take O(max) steps (e.g. f(1000, 1)), unlike the mod version's O(log).
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 s = 0, i = 0 while (true) i = i + 1 if (i mod 4 == 0) continue s = s + i if…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer d1 = 0, d2 = 0, t = 0 for…
- What will be the output of the following pseudocode? Integer c = 0 Procedure rec(Integer k) c = c + 1 if (k <= 1) return rec(k / 2) rec(k…
- What will be the output of the following pseudocode? // 32-bit signed integers with two's-complement wrap-around Integer x = 2147483647…
- What will be the output of the following pseudocode? Integer a = 13, b = 10 // 1101 and 1010 Print a & b, a | b, a ^ b, ~a // 32-bit two's…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 0) return 0 return f(n / 2) 10 + (n mod 2) End…