ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a, const void *b) {
    return strcmp(*(const char *const *)b, *(const char *const *)a);
}
int main(void) {
    const char *w[] = {"ab", "b", "abc"};
    qsort(w, 3, sizeof w[0], cmp);
    for (int i = 0; i < 3; i++) printf("%s ", w[i]);
}
  1. A. abc ab b
  2. B. b abc ab
  3. C. ab abc b
  4. D. b ab abc

Correct answer: B. b abc ab

The comparator swaps its operands, so the strings are sorted in descending lexicographic order: b, then abc, then ab.

More C Programming questions

Exams that ask this topic