GateAI
Recursion

GATE CSE 2011 · Q49

MCQ2 marksEasy

Time

What is the return value of the function foo when it is called as foo(513, 2)?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) 2

Recursion

The function foo(n, r) computes the sum of the digits of nn represented in base rr. When r=2r = 2, it computes the sum of the bits (i.e., the number of set bits / 11s) in the binary representation of nn.

Convert 513513 to binary:

513=512+1=29+20=(1000000001)2513 = 512 + 1 = 2^9 + 2^0 = (1000000001)_2

The number of set bits (11s) in the binary representation is 1+1=21 + 1 = 2.

Hence, foo(513, 2) returns 2, which corresponds to option (D).

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 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 · Q49 · MCQ

What is the return value of the function foo when it is called as foo(513, 2) ?

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.