GateAI
Recursion

GATE CSE 2017 Set 1 · Q36

MCQ2 marksMedium

Time

Consider the C functions foo and bar given below:int foo(int val) { int x = 0; while(val > 0) { x = x + foo(val--); } return val; } int bar(int val) { int x = 0; while(val > 0) { x = x + bar(val-1); } return val; }Invocations of foo (3) and bar (3) will result in :

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) Abnormal termination and infinite loop respectively.

Recursion

Let us analyze the behavior of both functions:

  1. For foo(3):

    • When foo(3) is called, val = 3 > 0 is true.

    • Inside the loop, it evaluates foo(val--). The postfix decrement val-- evaluates to 33 as the argument to the recursive call, before decrementing val.

    • Therefore, foo(3) immediately calls foo(3) again.

    • This causes an infinite sequence of recursive calls with the parameter 33, consuming stack frames continuously until memory runs out.

    • This results in a stack overflow, leading to abnormal termination (crash).

  2. For bar(3):

    • When bar(3) is called, it enters while(val > 0) and calls bar(2), which in turn calls bar(1).

    • Inside bar(1):

      • The loop condition val > 0 (1>01 > 0) is true.

      • It calls bar(0).

      • Inside bar(0), val = 0, so the loop while(val > 0) is skipped and bar(0) immediately returns 00.

      • Control returns to bar(1), where x = x + 0 is executed.

      • In bar(1), the value of val is still 11 because val - 1 was passed by value and val itself was never modified.

      • The loop in bar(1) continues with val = 1 > 0 and calls bar(0) repeatedly in an infinite loop without causing a stack overflow (as bar(0) returns immediately each time).

Hence, foo(3) results in abnormal termination, and bar(3) results in an infinite loop.

Thus, option (C) is the correct answer.

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 Q36 of the GATE CSE 2017 Set 1 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. 10 of them are 2-mark MCQs like this one, and GATE CSE 2017 Set 1 has 2.

How GATE tests Recursion

More from GATE CSE 2017 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2017 Set 1 · Q36 · MCQ

Consider the C functions foo and bar given below: int foo(int val) { int x = 0; while(val > 0) { x = x + foo(val--); } return val; } int bar(int val) { int x = 0; while(val > 0) { x = x + bar(val-1); } return val; } Invocations of foo (3) and bar (3) will result in :

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.