JavaScript is required

Biểu thức nào sau đây cho x có giá trị dương:

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;

Trả lời:

Đáp án đúng: A


The question asks which expression assigns a positive value to `x`. We need to consider the bitwise shift operators and the data type of `x`. * **Option 1: `int x = -1; x = x >>> 5;`** * `x = -1` (in binary, all bits are 1). * `x >>> 5` is an unsigned right shift by 5 bits. This shift pushes the last 5 bits out and inserts 5 zero bits at the beginning. Because `x` initially has all bits as 1, after the unsigned right shift, the upper bits are filled with 0, so `x` becomes a positive number. * **Option 2: `int x = -1; x = x >>> 32;`** * `x = -1`. * `x >>> 32` is equivalent to `x >>> 0`, because only the last 5 bits of the right-hand operand are used to calculate the shift amount in Java (since the data type of x is `int` - 32 bits). Therefore, `x` remains -1. * **Option 3: `byte x = -1; x = x >>> 5;`** * `byte x = -1`. * When `x` is a `byte`, it will be automatically promoted to `int` before performing the unsigned right shift. Therefore, `x >>> 5` is similar to option 1, i.e., the value will be positive. However, the data type of x is now `int`, not `byte` anymore. * **Option 4: `int x = -1; x = x >> 5;`** * `x = -1`. * `x >> 5` is a signed right shift. This shift pushes the last 5 bits out and inserts 5 sign bits (in this case, 1) at the beginning. Therefore, `x` remains a negative number. Thus, the correct answer is option 1, because after the unsigned right shift, `x` will have a positive value.

Tổng hợp 350 câu hỏi trắc nghiệm môn lập trình mạng có đáp án đầy đủ nhằm giúp các bạn dễ dàng ôn tập lại toàn bộ các kiến thức.


50 câu hỏi 60 phút

Câu hỏi liên quan