ProgrammingHardGATE CS · Campus placements
A qsort comparator compares double values with (x > y) - (x < y). After sorting {2.5, -1.0, 2.25}, which value is last?
- A. -1.0
- B. 2.25
- C. 2.5
- D. 0
Correct answer: C. 2.5
The ascending order is -1.0, 2.25, 2.5, so 2.5 is last. A comparator built from subtraction would also work here, but the three-way form avoids its overflow risk on large values.
More C Programming questions
- Merge sort merges two halves with n element comparisons, so T(n) = 2T(n/2) + n and T(1) = 0. What is T(8)?
- unsigned int x = 1u; x = ~x; What is x?
- unsigned int u = 5u; int s = -5; printf("%s", (u + s == 0) ? "yes" : "no"); What is printed?
- uint32t mask = 0; for (int i = 0; i <= 30; i += 3) mask |= 1u << i; How many set bits does mask have?
- unsigned int v = (unsigned int)(-2); printf("%X", v); What is printed?
- int a[3] = {1, 2, 3}; int p = a; int (q)[3] = &a; printf("%d", (q + 2) + (p + 1)); What is printed?