ProgrammingHardAmazon OA · Infosys SP

What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first)

What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first)
Integer a[5] = {2, 3, 5, 7, 8}, c = 0, s
for (each mask from 1 to 31)
    s = 0
    for (each i from 0 to 4)
        if ((mask >> i) & 1 == 1)
            s = s + a[i]
        end if
    end for
    if (s == 10)
        c = c + 1
    end if
end for
Print c
  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 3

Correct answer: D. 3

Each mask from 1 to 31 is a non-empty subset. Subsets with sum 10: {2, 8}, {3, 7}, {2, 3, 5}. c = 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic