Consider the function func shown below: int func(int num) { int count = 0; while (num) { count++; num>>= 1; } return (count); } The value returned by func(435) is …
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2014 Set 2 · Q10
Time
func shown below:int func(int num) {
int count = 0;
while (num) {
count++;
num>>= 1;
}
return (count);
}The value returned by func(435) is ________.Your answerNumerical answer
Solution
Official answer: 9
Programming in CThe function func counts the number of times num can be right-shifted by 1 until it becomes 0. This is equivalent to finding the total number of bits in the binary representation of num (i.e., ).
Convert to binary:
The binary representation has bits.
Trace of the loop:
After step 1: num = 217, count = 1
After step 2: num = 108, count = 2
After step 3: num = 54, count = 3
After step 4: num = 27, count = 4
After step 5: num = 13, count = 5
After step 6: num = 6, count = 6
After step 7: num = 3, count = 7
After step 8: num = 1, count = 8
After step 9: num = 0, count = 9
The loop terminates with count = 9.
Therefore, the returned value is 9.
Try the question first, then check your work.
Examiner trap
The loop right-shifts num by 1 bit in each iteration until num becomes 0.
This Programming in C previous year question is Q10 of the GATE CSE 2014 Set 2 paper. Find more GATE CSE PYQs on Programming in C 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 Programming in C 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.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 16 of them are 1-mark NAT questions like this one, and GATE CSE 2014 Set 2 has 2.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.