ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
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 A(m - 1, 1)
end if
return A(m - 1, A(m, n - 1))
End Function
Print A(2, 3)- A. 7
- B. 9
- C. 6
- D. 11
Correct answer: B. 9
This is the Ackermann function. A(1, n) = n + 2 and A(2, n) = 2n + 3, so A(2, 3) = 9. (Check: A(2, 0) = A(1, 1) = 3, A(2, 1) = A(1, 3) = 5, A(2, 2) = 7, A(2, 3) = 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 s = 0 for (each i from 1 to 4) for (each j from 1 to 4) if ((i j) mod 3 == 0)…
- What will be the output? (A do-while loop runs its body once before checking the condition; / is integer division.) Integer x = 0, c = 0…
- What will be the output of the following pseudocode? Integer p = 1 for (each i from 1 to 10) p = (p 3) mod 7 end for Print p
- The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.) Integer low = 0, high = 8…
- How many swaps does this pseudocode perform? (Arrays are indexed from 0.) Integer arr[5] = {5, 1, 4, 2, 8} Integer swaps = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n…