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

This question was previously asked in
GATE CS 2014 Official Paper: Shift 2
View all GATE CS Papers >

Answer (Detailed Solution Below) 9

Free
GATE CS Full Mock Test
5.3 K Users
65 Questions 100 Marks 180 Mins

Detailed Solution

Download Solution PDF

Concept:

((num>>= 1)== (num = num >> 1)) : implies a num is shifted one bit right.

\({\rm{Right\;shif\;formula}} = \frac{{{\rm{number}}}}{{{2^{\rm{i}}}}}\)

Here i means how many bits to shift right

Explanation:

Loop 1

Func(435) num = 435, count = 0

while(435) condition true

count = 1

num = 217

Loop 2

while217) condition true

count = 2,

num = 108

Loop 3

while(108) condition true

Count = 3

num = 54

Loop 4

while(54) condition true

Count = 4

 num = 27

Loop 5

while(27) condition true

Count = 5a

num = 13

Loop 6

while(13) condition true

Count = 6

num = 6

Loop 7

while(6) condition true

Count = 7

num = 3

Loop 8

while(3) condition true

Count = 8

num = 1

Loop 9

while(1) condition true

Count = 9

num = 0

Loop 10

while (0) condition false

Func returns value 9

 

 

Hence 9 is the correct answer.

Latest GATE CS Updates

Last updated on Jan 8, 2025

-> GATE CS 2025 Admit Card has been released on 7th January 2025.

-> The exam will be conducted on 1st February 2025 in 2 shifts.

-> Candidates applying for the GATE CE must satisfy the GATE Eligibility Criteria.

-> The candidates should have BTech (Computer Science). Candidates preparing for the exam can refer to the GATE CS Important Questions to improve their preparation.

-> Candidates must check their performance with the help of the GATE CS mock tests and GATE CS previous year papers for the GATE 2025 Exam.

More While Loop Questions

Hot Links: teen patti casino download teen patti master online teen patti wink teen patti wala game teen patti joy official