ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
int pc(int x) {
int c = 0;
while (x) { c += x & 1; x >>= 1; }
return c;
}
int cmp(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
if (pc(x) != pc(y)) return pc(x) - pc(y);
return x - y;
}
int main(void) {
int v[] = {7, 8, 3, 5};
qsort(v, 4, sizeof v[0], cmp);
for (int i = 0; i < 4; i++) printf("%d ", v[i]);
}- A. 8 3 7 5
- B. 8 3 5 7
- C. 3 5 8 7
- D. 8 5 3 7
Correct answer: B. 8 3 5 7
Popcounts are 3 for 7, 1 for 8, and 2 for both 3 and 5. The order is 8, then 3 and 5 by value, then 7.
More C Programming questions
- What is printed? c #include <stdio.h> int C(int n) { if (n == 0) return 1; int s = 0; for (int i = 0; i < n; i++) s += C(i) C(n - 1 - i)…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = (unsigned)-5; printf("%u", x >> 31); }
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = m; p++; printf("%d", p); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xA8; printf("%u", x & (0u - x)); }
- What is printed? c #include <stdio.h> int main(void) { unsigned short p = 1; for (int i = 0; i < 20; i++) p = 2; printf("%d", p); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xFFFFFFFEu; x += 3; printf("%u", x); }