GateAI
Recursion

GATE CSE 2021 Set 2 · Q49

NAT2 marksMedium

Time

Consider the following ANSI C program.#include <stdio.h> int foo(int x, int y, int q) { if ((x <= 0) && (y <= 0)) return q; if (x <= 0) return foo(x, y-q, q); if (y <= 0) return foo(x-q, y, q); return foo(x, y-q, q) + foo(x-q, y, q); } int main() { int r = foo(15,15,10); printf("%d", r); return 0; }The output of the program upon execution is ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 60

Recursion

We need to trace the execution of foo(15, 15, 10):

  1. Call foo(15, 15, 10): Since x=15>0x = 15 > 0 and y=15>0y = 15 > 0, it executes:

    foo(15,15,10)=foo(15,5,10)+foo(5,15,10)\text{foo}(15, 15, 10) = \text{foo}(15, 5, 10) + \text{foo}(5, 15, 10)
  2. Evaluate foo(15, 5, 10):

    foo(15,5,10)=foo(15,−5,10)+foo(5,5,10)\text{foo}(15, 5, 10) = \text{foo}(15, -5, 10) + \text{foo}(5, 5, 10)
    • For foo(15, -5, 10): since y≤0y \le 0, it calls foo(15 - 10, -5, 10) = foo(5, -5, 10).

      • For foo(5, -5, 10): since y≤0y \le 0, it calls foo(-5, -5, 10).

      • For foo(-5, -5, 10): since x≤0x \le 0 and y≤0y \le 0, it returns q=10q = 10.

      • Thus, foo(15, -5, 10) = 10.

    • For foo(5, 5, 10):

      foo(5,5,10)=foo(5,−5,10)+foo(−5,5,10)\text{foo}(5, 5, 10) = \text{foo}(5, -5, 10) + \text{foo}(-5, 5, 10)
      • foo(5, -5, 10) returns 1010.

      • foo(-5, 5, 10): since x≤0x \le 0, it calls foo(-5, -5, 10) which returns 1010.

      • Thus, foo(5, 5, 10) = 10 + 10 = 20.

    • Therefore:

      foo(15,5,10)=10+20=30\text{foo}(15, 5, 10) = 10 + 20 = 30
  3. Evaluate foo(5, 15, 10): Due to symmetry with respect to xx and yy:

    foo(5,15,10)=30\text{foo}(5, 15, 10) = 30
  4. Final Value:

    foo(15,15,10)=30+30=60\text{foo}(15, 15, 10) = 30 + 30 = 60

Hence, the output printed is 60.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Recursion previous year question is Q49 of the GATE CSE 2021 Set 2 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 5 of them are 2-mark NAT questions like this one, and it is the only one in GATE CSE 2021 Set 2.

How GATE tests Recursion

More from GATE CSE 2021 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 2 · Q49 · NAT

Consider the following ANSI C program. #include int foo(int x, int y, int q) { if ((x The output of the program upon execution is …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.