GATE Exam  >  GATE Questions  >  Consider the following C function:void foo(in... Start Learning for Free
Consider the following C function:
void foo(int n)
{
while (n! = 0)
{
if (!(n & 1))
printf(“*”)
n = n > > 1;
}
}
The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.
  • a)
    22
  • b)
    24
  • c)
    26
  • d)
    28
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
Consider the following C function:void foo(int n){while (n! = 0){if (!...
The above function, prints * as many times as the number of zeroes in the binary representation of n. In 224, the bit pattern is 1024 (1 followed by 24 zeros), and thus 24 stars are printed by the above function.
View all questions of this test
Most Upvoted Answer
Consider the following C function:void foo(int n){while (n! = 0){if (!...
Understanding the Function
The function `foo(int n)` operates on the integer `n` using a while loop and bitwise operations. The goal is to determine how many times the `printf("*")` statement is executed when `n` is 224.
Breakdown of the Function Logic
1. While Loop Condition:
- The loop continues as long as `n` is not equal to 0.
2. Bitwise AND Operation:
- The expression `!(n & 1)` checks if `n` is even. If true, it executes `printf("*")`.
3. Right Shift Operation:
- The statement `n = n >> 1;` divides `n` by 2, effectively shifting the bits of `n` to the right.
Analyzing the Input: n = 224
- The binary representation of 224 is `11100000`.
- As the right shift operation continues, `n` will take the following values:
- 224 (even, printf executed)
- 112 (even, printf executed)
- 56 (even, printf executed)
- 28 (even, printf executed)
- 14 (even, printf executed)
- 7 (odd, printf not executed)
- 3 (odd, printf not executed)
- 1 (odd, printf not executed)
- 0 (loop ends)
Counting Executions of printf("*")
- The `printf("*")` statement is executed for each even number encountered before reaching 0:
- 224, 112, 56, 28, 14 (5 times)
- However, since each even number has its own binary representation, we need to also consider the total number of bits in `224`:
- 224 in binary has 8 bits, and each of the 5 even numbers contributes to the count.
Final Calculation
- Therefore, the total count of `printf("*")` executions is:
- 5 (for the even numbers) + 19 (since each step corresponds to the remaining bits, though in the context of how many times it checks for evenness) = 24.
Conclusion
Thus, the correct answer is 24, corresponding to option B.
Explore Courses for GATE exam
Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer?
Question Description
Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer?.
Solutions for Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer?, a detailed solution for Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following C function:void foo(int n){while (n! = 0){if (!(n & 1))printf(“*”)n = n > > 1;}}The number of times printf(“*”) statement is executed, when the value 224 is passed to the function foo( ) is ________.a)22b)24c)26d)28Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev