ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer Function f(Integer a, Integer b)
if (a == 0) return b
return f(b mod a, a)
End Function
Print f(84, 36)- A. 12
- B. 36
- C. 6
- D. 84
Correct answer: A. 12
Calls: f(84, 36) → f(36 mod 84 = 36, 84) → f(84 mod 36 = 12, 36) → f(0, 12) → 12. It is Euclid's gcd with the arguments in the other order; the first call simply swaps them.
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 a[] = {4, 7, 1, 9, 3} for (i = 0 to 3) if (a[i] > a[i + 1]) swap(a[i], a[i +…
- What will be the output of the following pseudocode? Integer n = 5, s = 0 for (i = 1 to n) for (j = 1 to i) if ((i + j) mod 2 == 0) s = s…
- What will be the output of the following pseudocode? Integer v = 1 for (k = 0 to 4) v = v 2 + k end for Print v
- What will be the output of the following pseudocode? Integer calls = 0 Integer Function f(Integer n) calls = calls + 1 if (n <= 2) return…
- What will be the output of the following pseudocode? Integer a = 5, b, c, d b = a++ // post-increment c = ++a // pre-increment d = a + b +…
- What will be the output of the following pseudocode? Integer a[5] = {2, 4, 6, 8, 10} for (i = 1 to 4) a[i] = a[i] + a[i - 1] end for Print a