ProgrammingVery hardTCS NQT · Accenture
What is printed? Each struct has an int a and an int b.
What is printed? Each struct has an int a and an int b.
#include <stdio.h>
#include <stdlib.h>
struct P { int a, b; };
int cmp(const void *x, const void *y) {
const struct P *p = x, *q = y;
if (p->a != q->a) return p->a - q->a;
return q->b - p->b;
}
int main(void) {
struct P v[] = {{2, 1}, {1, 9}, {2, 0}};
qsort(v, 3, sizeof v[0], cmp);
for (int i = 0; i < 3; i++) printf("%d %d ", v[i].a, v[i].b);
}- A. 1 9 2 0 2 1
- B. 1 9 2 1 2 0
- C. 2 1 2 0 1 9
- D. 2 0 2 1 1 9
Correct answer: B. 1 9 2 1 2 0
The primary key a ascending gives (1, 9) first. Among the two rows with a = 2, the secondary key b descending puts (2, 1) before (2, 0).
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 11, y = x & (x + 1); printf("%u", y); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int (p)[3] = 0; printf("%zu", sizeof p); }
- What is printed? Assume the array has space for 5 characters. c #include <stdio.h> int main(void) { char s[5] = "abcd"; printf("%zu"…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'A' == 65); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0; x--; printf("%u", x >> 31); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3}; printf("%d", (a + 1) + (1 + a)); }