ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including the empty set. It prints how many of these subsets have an even sum. What is printed?

The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including the empty set. It prints how many of these subsets have an even sum. What is printed?
count = 0
for mask = 0 to 7
    s = sum of (i+1) for each bit i set in mask
    if s mod 2 == 0 then count = count + 1
end for
Print count
  1. A. 5
  2. B. 3
  3. C. 2
  4. D. 4

Correct answer: D. 4

The sums of the eight subsets are 0, 1, 2, 3, 3, 4, 5 and 6, of which 0, 2, 4 and 6 are even, giving 4. Counting sums of at least 3 gives 5, leaving out the empty set gives 3, and counting sums divisible by 4 gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic