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
GATE CSE 2011 · Q49
Time
foo when it is called as foo(513, 2)?Select one answerSingle correct
Solution
Official answer: D) 2
RecursionThe function foo(n, r) computes the sum of the digits of represented in base . When , it computes the sum of the bits (i.e., the number of set bits / s) in the binary representation of .
Convert to binary:
The number of set bits (s) in the binary representation is .
Hence, foo(513, 2) returns 2, which corresponds to option (D).
Try the question first, then check your work.
Examiner trap
Represent 513 in binary (base 2) and count the number of 1s in its binary representation.
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
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.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
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 RecursionIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.