GateAI
Recursion

GATE CSE 2011 · Q48

MCQ2 marksEasy

Time

Common Data for Questions 48 and 49

Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; }What is the return value of the function foo when it is called as foo(345, 10)?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 12

Recursion

The recursive function foo(n, r) computes the sum of the digits of n in base r.

Tracing the execution for foo(345, 10):

  • foo(345, 10) returns (345 % 10) + foo(34, 10) = 5 + foo(34, 10)

  • foo(34, 10) returns (34 % 10) + foo(3, 10) = 4 + foo(3, 10)

  • foo(3, 10) returns (3 % 10) + foo(0, 10) = 3 + foo(0, 10)

  • foo(0, 10) hits the base case (n <= 0) and returns 0

Summing the values:

5+4+3+0=125 + 4 + 3 + 0 = 12

Therefore, the return value is 12, which corresponds to option (B).

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 Q48 of the GATE CSE 2011 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 2011 has 2.

How GATE tests Recursion

More from GATE CSE 2011

Share this solution

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

GateAI solution cardGATE CSE 2011 · Q48 · MCQ

Common Data for Questions 48 and 49: Consider the following recursive C function that takes two arguments. unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; } What is the return value of the function foo when it is called as foo(345, 10) ?

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.