ProgrammingHardTCS NQT Advanced · Capgemini

What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the new value)

What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the new value)
Integer c = 0, hits = 0
for (each k from 1 to 6)
    if (k mod 2 == 0 OR ++c > 2)
        hits = hits + 1
    end if
end for
Print c, hits
  1. A. 6 4
  2. B. 3 3
  3. C. 2 4
  4. D. 3 4

Correct answer: D. 3 4

OR stops as soon as the left side is true, so ++c runs only for odd k (1, 3, 5): c becomes 1, 2, 3, and only the last makes c > 2 true. hits = 3 even k + 1 = 4, c = 3: 3 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic