ProgrammingHardProduct company OA
What is printed?
What is printed?
struct E { char n; int age; } e[] = {{'A', 30}, {'B', 25}, {'C', 35}};
int cmp(const void *x, const void *y) {
return ((const struct E *)x)->age - ((const struct E *)y)->age;
}
int main(void) {
qsort(e, 3, sizeof e[0], cmp);
printf("%c%c%c", e[0].n, e[1].n, e[2].n);
}- A. ABC
- B. CAB
- C. CBA
- D. BAC
Correct answer: D. BAC
qsort moves whole structs (sizeof e[0] each) and orders them by the comparator, here ascending age: 25 (B), 30 (A), 35 (C). 'CAB' would need the comparison reversed.
More C Programming questions
- What is printed? c #define SCALE(x) (x factor) int factor = 3; int f(void) { int factor = 10; return SCALE(2); } int main(void) {…
- What is printed? c int i = 0, n = 0; while (i++, i < 4) n += i; printf("%d %d", n, i);
- What is the range of values of a signed 8-bit two's-complement integer?
- What is printed? c unsigned x = 8; if (x & (x - 1) == 0) puts("pow2"); else puts("not");
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, r[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++)…
- What is printed? c const char s = "banana", p = s; int c = 0; while ((p = strchr(p, 'a')) != NULL) { c++; p++; } printf("%d", c);