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) {
const char *x = *(const char *const *)a, *y = *(const char *const *)b;
size_t lx = strlen(x), ly = strlen(y);
if (lx != ly) return lx < ly ? -1 : 1;
return strcmp(x, y);
}
int main(void) {
const char *w[] = {"pear", "fig", "apple", "kiwi", "date"};
qsort(w, 5, sizeof w[0], cmp);
for (int i = 0; i < 5; i++) printf("%s ", w[i]);
}- A. fig date pear kiwi apple
- B. fig date kiwi pear apple
- C. apple date fig kiwi pear
- D. fig kiwi date pear apple
Correct answer: B. fig date kiwi pear apple
Shorter words come first, and words of the same length are ordered lexicographically. So fig (3 letters) precedes date, kiwi and pear (4 letters), which precede apple (5 letters).
More C Programming questions
- How many subarrays of {2, 1, 4, 3, 1, 2, 4} have a sum of exactly 7? Compute with this program. c #include <stdio.h> int main(void) { int…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3, dq[8], head = 0, tail = 0, total =…
- How many values x in 0..255 satisfy (x & (uint8t)-x) == x? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> int f(int n) { static int calls = 0; calls++; if (n <= 0) return calls; int r = f(n - 2); return r…
- What is printed? c #include <stdio.h> struct P { int v; }; int main(void) { struct P arr[3] = {{1}, {2}, {3}}; int sum = 0; for (struct P…
- What is printed? c #include <stdio.h> int main(void) { char c = 'a'; printf("%d %d", c + 1 == 'b', '9' - '0'); }