ProgrammingHardInfosys SP · Goldman Sachs OA

What will be the output of the following pseudocode? (^ is bitwise XOR)

What will be the output of the following pseudocode? (^ is bitwise XOR)
Integer s = 0
for (each i from 0 to 15)
    s = s + (i ^ 5)
end for
Print s
  1. A. 120
  2. B. 80
  3. C. 125
  4. D. 40

Correct answer: A. 120

For 0 ≤ i ≤ 15, i ^ 5 is again a number from 0 to 15, and different i give different results, so the values are just 0 to 15 in another order. The sum is 0 + 1 + … + 15 = 120.

More Pseudocode and Code Tracing questions

Exams that ask this topic