ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
return (y > x) - (y < x);
}
int main(void) {
int v[] = {2, 9, 1, 4};
qsort(v, 4, sizeof v[0], cmp);
for (int i = 0; i < 4; i++) printf("%d ", v[i]);
}- A. 9 4 2 1
- B. 1 2 4 9
- C. 4 9 2 1
- D. 9 2 4 1
Correct answer: A. 9 4 2 1
The comparator returns a positive value when y is larger than x, so the array ends up in descending order: 9, 4, 2, 1.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned s = 0; for (unsigned b = 1; b; b <<= 1) s += 1; printf("%u", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (3 & 1) == 1 ? 'y' : 'n'); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (7 & 5) | (2 ^ 6) << 1); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 5 > 3 ? 2 > 1 ? 7 : 8 : 9); }
- What is printed? c #include <stdlib.h> #include <stdio.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…
- What is printed? c #include <stdio.h> unsigned fib(unsigned n) { unsigned a = 0, b = 1; for (unsigned i = 0; i < n; i++) { unsigned t = a…