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]);
}- A. abc ab b
- B. b abc ab
- C. ab abc b
- 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
- What index is printed when bsearch looks up "cat" in the sorted array {"ant", "bee", "cat", "dog"}? c #include <stdio.h> #include…
- How many ordered ways are there to write 10 as a sum of 1s, 2s and 3s? Compute with this program. c #include <stdio.h> int f(int n) { if…
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 44; while (n & (n - 1)) n &= n - 1; printf("%u", n); }
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 0; printf("%d", (n & (n - 1)) == 0); }
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint32t h = 2166136261u; h ^=…
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> struct Rec { char name[4]; int score; }; int cmp(const void…