ProgrammingVery hardProduct company OA

What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.)

What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.)
Integer n = 10, c = 1, odd = 0
for (k = 0 to n)
    if (c mod 2 == 1)
        odd = odd + 1
    end if
    c = c * (n - k) / (k + 1)
end for
Print odd
  1. A. 6
  2. B. 11
  3. C. 2
  4. D. 4

Correct answer: D. 4

c runs through row 10 of Pascal's triangle: 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1. The odd entries are 1, 45, 45, 1 → 4. Lucas' theorem predicts this: 10 = 1010₂ has two 1-bits, so the row has 2² = 4 odd entries. '11' counts every entry.

More Pseudocode and Code Tracing questions

Exams that ask this topic