ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (each y from 1890 to 2010)
    if ((y mod 4 == 0 AND y mod 100 != 0) OR y mod 400 == 0)
        c = c + 1
    end if
end for
Print c
  1. A. 30
  2. B. 31
  3. C. 28
  4. D. 29

Correct answer: D. 29

Multiples of 4 from 1892 to 2008: (2008 − 1892)/4 + 1 = 30; 1900 fails the century rule, 2000 passes it (divisible by 400), so 29. 30 ignores the century rule, 28 also drops 2000, and 31 is (2010 − 1890)/4 + 1, wrongly treating 1890 as a leap year.

More Pseudocode and Code Tracing questions

Exams that ask this topic