Java OCA OCP Practice Question 544

Question

Which of the following expressions results in a positive value in x?

  • A. int x = -1; x = x >>> 5;
  • B. int x = -1; x = x >>> 32;
  • C. byte x = -1; x = x >>> 5;
  • D. int x = -1; x = x >> 5;


A.

Note

Option A uses the unsigned right shift, so 0s are shifted into the most significant positions.

Any two's-complement number with a 0 in the most significant position is positive.

Option B shifts by 32 % 32 = 0 positions, so there is no effect.

Option C doesn't compile, because "x >>> 5" is an int, which can't be assigned to a byte.

Option D applies the signed shift to a negative operand, so the result is negative.




PreviousNext

Related