ProgrammingHardProduct company OA
What is printed?
What is printed?
char *w[] = {"pear", "fig", "apple", "kiwi"};
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3 - i; j++)
if (strlen(w[j]) > strlen(w[j + 1])) {
char *t = w[j]; w[j] = w[j + 1]; w[j + 1] = t;
}
printf("%s %s %s", w[0], w[1], w[3]);- A. fig pear apple
- B. fig kiwi apple
- C. fig pear kiwi
- D. apple pear fig
Correct answer: A. fig pear apple
This bubble-sorts the pointers by string length, swapping only when strictly greater, so equal lengths keep their order (it is stable). Lengths 4, 3, 5, 4 become fig(3), pear(4), kiwi(4), apple(5). 'kiwi' second would need an unstable sort; only pointers move, the strings themselves are untouched.
More C Programming questions
- What is printed? c int score = 73; char g = score >= 90 ? 'A' : score >= 75 ? 'B' : score >= 60 ? 'C' : 'D'; printf("%c%d", g, g - 'A');
- Which of these statements about C (not C++) are true? (i) A const int variable at file scope is a constant expression, so it can size a…
- Which of these is NOT allowed for a variable declared with the register storage class in C?
- What does p(5) print? c void p(int n) { if (n <= 0) return; p(n - 2); printf("%d", n); p(n - 3); }
- Which statement about this C declaration is true? c char a[5] = "hello";
- What is printed? c #define CUBE(x) ((x) (x) (x)) int n = 0; int f(void) { n++; return 2; } int main(void) { int r = CUBE(f()); printf("%d…