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);
}
  1. A. bob amy zed
  2. B. zed bob amy
  3. C. amy zed bob
  4. 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

Exams that ask this topic