ProgrammingVery hardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person { char name[4]; int age; };
int cmp(const void *a, const void *b) {
const struct Person *x = a, *y = b;
if (x->age != y->age) return x->age - y->age;
return strcmp(x->name, y->name);
}
int main(void) {
struct Person p[] = {{"ann", 30}, {"bob", 25}, {"cat", 30}, {"dan", 25}};
qsort(p, 4, sizeof p[0], cmp);
for (int i = 0; i < 4; i++) printf("%s ", p[i].name);
}- A. bob ann dan cat
- B. bob dan ann cat
- C. dan bob ann cat
- D. bob dan cat ann
Correct answer: B. bob dan ann cat
The age is the primary key and the name breaks ties, so the order is bob, dan (25), then ann, cat (30). Sorting by name alone gives a different order.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2, 5, 4}, c = 0; for (int i = 0; i < 5; i++) for (int j = i + 1; j…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t m = 0x0F; for (int i = 0; i < 8; i++) m = (uint8t)((m <<…
- What is printed? c #include <stdio.h> int main(void) { int x = 6; printf("%d", x & -x); }
- What is printed? c #include <stdio.h> int main(void) { const char s = " a bb ccc "; int words = 0, in = 0; for (; s; s++) { if (s == ' ')…
- What is printed? c #include <stdio.h> int ways(int n) { if (n == 0) return 1; if (n < 0) return 0; return ways(n - 1) + ways(n - 3); } int…
- What is printed? c #include <stdint.h> #include <stdio.h> uint64t pw(uint64t b, uint64t e, uint64t m) { uint64t r = 1; b %= m; while (e) {…