ProgrammingHardTCS NQT · Accenture pattern
What will be the output? (calls counts every call, including the first.)
What will be the output? (calls counts every call, including the first.)
Integer calls = 0
Function fun(Integer a, Integer b)
calls = calls + 1
if (a == 0)
return b
end if
return fun(b mod a, a)
End Function
Print fun(21, 13), calls- A. 1 7
- B. 1 6
- C. 13 8
- D. 1 8
Correct answer: D. 1 8
The first call swaps the pair because 13 mod 21 = 13: (21,13) → (13,21) → (8,13) → (5,8) → (3,5) → (2,3) → (1,2) → (0,1). That is 8 calls, returning gcd = 1. Missing the first swap call gives 7.
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 c = 0 for (i = 1; i < 64; i = i 2) for (j = i; j < 64; j = j + i) c = c + 1…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[4] = {2, 3, 1, 0} for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Function f(Integer n) if (n > 100) return n - 10 end if return f(f(n + 11)) End…
- What will be the output of the following pseudocode? Function A(Integer m, Integer n) if (m == 0) return n + 1 end if if (n == 0) return…