ProgrammingHardInfosys SP · Amazon OA

What will be the output of the following pseudocode? (<< is left shift, | is bitwise OR, & is bitwise AND)

What will be the output of the following pseudocode? (<< is left shift, | is bitwise OR, & is bitwise AND)
Integer x = 0
for (each i from 0 to 4)
    x = (x << 2) | (i & 3)
end for
Print x
  1. A. 108
  2. B. 228
  3. C. 27
  4. D. 120

Correct answer: A. 108

Each step appends two bits, i.e. one base-4 digit i & 3: the digits are 0, 1, 2, 3, 0. So x = 01230 in base 4 = 64 + 32 + 12 + 0 = 108.

More Pseudocode and Code Tracing questions

Exams that ask this topic