ProgrammingHardTCS NQT · Accenture

What is printed?

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

Correct answer: D. 3

The longest block of consecutive 1s in 1101110111 has length 3.

More C Programming questions

Exams that ask this topic