ProgrammingVery hardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Rec { char name[4]; int score; };
int cmp(const void *a, const void *b) {
const struct Rec *x = a, *y = b;
if (x->score != y->score) return y->score - x->score;
return strcmp(x->name, y->name);
}
int main(void) {
struct Rec r[] = {{"zed", 50}, {"amy", 70}, {"bob", 50}};
qsort(r, 3, sizeof r[0], cmp);
for (int i = 0; i < 3; i++) printf("%s ", r[i].name);
}- A. bob amy zed
- B. zed bob amy
- C. amy zed bob
- D. amy bob zed
Correct answer: D. amy bob zed
The higher score amy (70) comes first, and the two scores of 50 are ordered by name, so bob precedes zed.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 6; printf("%u", (x >> 1) ^ x); }
- What is printed? c #include <stdio.h> int main(void) { int x = 7; printf("%d", (x << 3) + x); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xB, p = 0; for (; x; x &= x - 1) p ^= 1; printf("%u", p); }
- What is printed? c #include <stdio.h> int sum(int m[][3], int r) { int s = 0; for (int i = 0; i < r; i++) for (int j = 0; j < 3; j++) s +=…
- What is printed? Assume long is 8 bytes and int is 4 bytes. c #include <stdio.h> int main(void) { printf("%zu %zu", sizeof(1L)…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 10 / 3 3 + 10 % 3); }