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[] = {"kiwi", "fig", "banana", "plum"};
qsort(w, 4, sizeof w[0], cmp);
for (int i = 0; i < 4; i++) printf("%s ", w[i]);
}- A. banana kiwi plum fig
- B. banana plum kiwi fig
- C. fig kiwi plum banana
- D. banana fig kiwi plum
Correct answer: A. banana kiwi plum fig
Longer strings come first, so banana (6) precedes the length-4 words, and fig (3) is last. Among kiwi and plum, strcmp puts kiwi first.
More C Programming questions
- How many ordered ways are there to write 10 as a sum of 1s and 4s? Compute with this program. c #include <stdio.h> int f(int n) { if (n ==…
- What is printed? c #include <stdio.h> int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main(void) { printf("%d"…
- What is printed? c #include <stdio.h> int rev(int n, int acc) { return n == 0 ? acc : rev(n / 10, acc 10 + n % 10); } int main(void) {…
- What is printed? The program checks whether the low four bits of each argument form a binary palindrome. c #include <stdio.h> unsigned…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 1, r = 0; for (int i = 0; i < 32; i++)…
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 1000; int p = -1; while (n) { n >>= 1; p++; } printf("%d", p); }