ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
int x = 181, c = 0;
while (x) {
x &= x - 1;
c++;
}
printf("%d", c);- A. 4
- B. 8
- C. 3
- D. 5
Correct answer: D. 5
x & (x - 1) clears the lowest set bit, so the loop runs once per 1-bit (Kernighan's bit count). 181 = 10110101 in binary has five 1s, so c = 5.
More C Programming questions
- What is printed? c printf("%d", 1 << 3 + 1);
- On a typical compiler where int is 4 bytes and 4-byte aligned, what is printed? c struct S { char c; int i; char d; }; struct T { int i…
- With 4-byte int, consider int (p)[5]; Which is correct?
- int a[3][4]; is stored in row-major order starting at address 1000, with 4-byte int. What is the address of a[2][1]?
- What is printed? c unsigned char c = 250; c += 10; printf("%d", c);
- What does f(3) print? c void f(int n) { if (n > 0) { f(n - 1); printf("%d ", n); f(n - 1); } }