GateAI
Programming in C

GATE CSE 2014 Set 2 · Q10

NAT1 markEasy

Time

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 ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 9

Programming in C

The 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., ⌊log⁡2(num)⌋+1\lfloor\log_2(\text{num})\rfloor + 1).

Convert 435435 to binary:

435=256+128+32+16+2+1=(110110011)2435 = 256 + 128 + 32 + 16 + 2 + 1 = (110110011)_2

The binary representation (110110011)2(110110011)_2 has 99 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.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

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 Programming in C 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.

Programming in C in GATE CSE

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 C

More from GATE CSE 2014 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2014 Set 2 · Q10 · NAT

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.