ProgrammingHardProduct company OA

What is printed?

What is printed?
for (int k = 0; k < 3; k++) {
    static int s[3];
    int a[3] = {0};
    s[k] = a[k] = k + 1;
    printf("%d%d ", s[0] + s[1] + s[2], a[0] + a[1] + a[2]);
}
  1. A. 11 32 33
  2. B. 11 32 63
  3. C. 11 22 33
  4. D. 11 33 66

Correct answer: B. 11 32 63

The static array is created once and keeps earlier writes: its sums are 1, 3, 6. The automatic array is re-created and zeroed on every pass, so it holds only the current element: 1, 2, 3. Each pass prints the two sums side by side.

More C Programming questions

Exams that ask this topic