ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {1, 2, 3, 4, 5, 6}, best = 0;
    for (int i = 0; i + 3 <= 6; i++) {
        int s = a[i] + a[i + 1] + a[i + 2];
        if (s > best) best = s;
    }
    printf("%d", best);
}
  1. A. 12
  2. B. 15
  3. C. 18
  4. D. 6

Correct answer: B. 15

The window sums are 6, 9, 12 and 15, so the largest is 15 from the last window.

More C Programming questions

Exams that ask this topic