ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
    return *(const unsigned char *)a - *(const unsigned char *)b;
}
int main(void) {
    char s[] = "banana";
    qsort(s, 6, 1, cmp);
    printf("%s", s);
}
  1. A. nnbaaa
  2. B. abnnaa
  3. C. aaannb
  4. D. aaabnn

Correct answer: D. aaabnn

Sorting the bytes ascending puts the three a's first, then b, then the two n's, giving aaabnn.

More C Programming questions

Exams that ask this topic