StreakPeaked· Practice

ExamsGATETechnical

What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f (int &x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; }

  1. 3024
  2. 6561
  3. 55440
  4. 161051

Correct answer: 6561

Solution

Because x is passed by reference, all recursive frames share it. The recursion increments x to 6,7,8,9 until c reaches 0, so at unwind x=9 everywhere; the product is 1 * 9 * 9 * 9 * 9 = 9^4 = 6561, not 161051.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →