Consider the following two functions. The output printed when fun1(5) is…

2017

Consider the following two functions.    

The output printed when fun1(5) is called is

Answer: A. 53423122233445Key idea: trace the mutual recursion and remember that ++n in the second function increments n before the recursive call, affecting subsequent prints. fun1(5)…

  1. A.

    53423122233445

  2. B.

    53423120112233

  3. C.

    53423122132435

  4. D.

    53423120213243

Attempted by 73 students.

Show answer & explanation

Correct answer: A

Key idea: trace the mutual recursion and remember that ++n in the second function increments n before the recursive call, affecting subsequent prints.

  1. fun1(5) prints 5

  2. fun2(3) prints 3

  3. fun2 increments n to 4 and calls fun1(4); fun1(4) prints 4

  4. fun2(2) prints 2

  5. fun2 increments to 3 and calls fun1(3); fun1(3) prints 3

  6. fun2(1) prints 1

  7. fun2 increments to 2 and calls fun1(2); fun1(2) prints 2

  8. fun1(2) calls fun2(0) which returns immediately, then fun1(2) prints 2 (second time)

  9. Return to fun2(1): it now prints its n (which was incremented to 2), producing another 2

  10. Return to fun1(3): it prints 3

  11. Return to fun2(2): it prints its n (which is 3)

  12. Return to fun1(4): it prints 4

  13. Return to fun2(3): it prints its n (which is 4)

  14. Return to fun1(5): it prints 5

Final output: 53423122233445

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…