ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {1, 4, 6}, b[] = {2, 3, 8, 9};
    int m[7], i = 0, j = 0, k = 0;
    while (i < 3 && j < 4) m[k++] = a[i] <= b[j] ? a[i++] : b[j++];
    while (i < 3) m[k++] = a[i++];
    while (j < 4) m[k++] = b[j++];
    printf("%d", m[4]);
}
  1. A. 4
  2. B. 8
  3. C. 3
  4. D. 6

Correct answer: D. 6

The merged array is 1, 2, 3, 4, 6, 8, 9, so index 4 holds 6. The second merge loop copies the remaining elements of a when b is exhausted.

More C Programming questions

Exams that ask this topic