ProgrammingVery hardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
int v[] = {30, 10, 20};
int cmp(const void *a, const void *b) {
    int i = *(const int *)a, j = *(const int *)b;
    return (v[i] > v[j]) - (v[i] < v[j]);
}
int main(void) {
    int idx[] = {0, 1, 2};
    qsort(idx, 3, sizeof idx[0], cmp);
    for (int i = 0; i < 3; i++) printf("%d ", idx[i]);
}
  1. A. 1 2 0
  2. B. 0 1 2
  3. C. 2 0 1
  4. D. 1 0 2

Correct answer: A. 1 2 0

The array is sorted by the values it indexes, not by the indices. Values 10, 20, 30 sit at indices 1, 2, 0, so that is the printed order.

More C Programming questions

Exams that ask this topic