ProgrammingHardTCS NQT · Accenture pattern

How many of the following pseudocode statements print 1? (/ is integer division; & is bitwise AND; ^ is bitwise XOR.) (i) Print (7 / 2) * 2 == 7 (ii) Print (6 & 3) == 7 (iii) Print (5 ^ 2) == 25 (iv) Print (-8 >> 1) == -4, assuming an arithmetic right shift

  1. A. Only three
  2. B. All four
  3. C. Only one
  4. D. Only two

Correct answer: C. Only one

Only (iv) prints 1: an arithmetic shift keeps the sign, so -8 >> 1 = -4. (i) prints 0 because 7 / 2 = 3 and 3 × 2 = 6. (ii) prints 0 because 110 & 011 = 010 = 2 (7 would be OR). (iii) prints 0 because ^ is XOR: 101 ^ 010 = 111 = 7, not 25.

More Pseudocode and Code Tracing questions

Exams that ask this topic