# Practice exam question

sasank manohar

Ranch Hand

Posts: 186

posted 16 years ago

This below question is from the website -http://209.242.122.83/JavaCertification.html

What is the result of the following fragment of code?

(byte)0x81>>2

a. 0x20

b. 0x3FFFFFE0

c. 0XE0

d. 0xFFFFFFE0

I don't know the answer, but I want to know that, will this type of questions appear in the real exam

Thank you.......

What is the result of the following fragment of code?

(byte)0x81>>2

a. 0x20

b. 0x3FFFFFE0

c. 0XE0

d. 0xFFFFFFE0

I don't know the answer, but I want to know that, will this type of questions appear in the real exam

Thank you.......

"SCJP5 | SCWCD5| DEVELOPER"

sasank manohar

Ranch Hand

Posts: 186

Hema

Greenhorn

Posts: 12

posted 16 years ago

Hi Sdev,

I'm a novice myself, but here is how I do the shifting.

Refer RHE, It says

If a binary number is shifted left one position, the effect of the shift is to double the original number. So the result is as if the number is multiplied by 2,4, 8,16 & so on.

Similarly in shifting right , the result is as if the number is

divided by 2, 4, 8 & so on..Please note, the case is different for negative numbers.

If we take yr case, (byte)0x81>>2

1.Convert 0X81 to decimal. The equivalent decimal value is 129.

2.Shifting 129 >> 2, is equivalent to dividing 129 by 4. The

value will be 32 (not 32.5)

3. Convert 32 to Hex. The value will be 0X20.

So the answer will be the choice a. 0X20

Please feel free to correct me if I am wrong.

-Hema

I'm a novice myself, but here is how I do the shifting.

Refer RHE, It says

If a binary number is shifted left one position, the effect of the shift is to double the original number. So the result is as if the number is multiplied by 2,4, 8,16 & so on.

Similarly in shifting right , the result is as if the number is

divided by 2, 4, 8 & so on..Please note, the case is different for negative numbers.

If we take yr case, (byte)0x81>>2

1.Convert 0X81 to decimal. The equivalent decimal value is 129.

2.Shifting 129 >> 2, is equivalent to dividing 129 by 4. The

value will be 32 (not 32.5)

3. Convert 32 to Hex. The value will be 0X20.

So the answer will be the choice a. 0X20

Please feel free to correct me if I am wrong.

-Hema

Vivek Shrivastava

Ranch Hand

Posts: 277

posted 16 years ago

Hi,

sdev and hema please try following link. it will help u lot.

http://www.javaranch.com/ubb/Forum24/HTML/001250.html

sdev and hema please try following link. it will help u lot.

http://www.javaranch.com/ubb/Forum24/HTML/001250.html

sanjay gautam

Ranch Hand

Posts: 35

posted 16 years ago

hello evreybody,

the answer for this question will be 0XE0.

explanation:---

1) the given number ix in hex.

2)value for this in binary is 10000001.

3)after >>2, it will come out 11100000 bcoz during right shiftvacant bits are filled by 8th bit. i.e. if it is zero vacant bits will be filled by zero and if it is one vacants bits will be filled by one.

4)now 11100000 is to be changed in hex again and it is 0XE0.

if i am wrong write me.

gautam

the answer for this question will be 0XE0.

explanation:---

1) the given number ix in hex.

2)value for this in binary is 10000001.

3)after >>2, it will come out 11100000 bcoz during right shiftvacant bits are filled by 8th bit. i.e. if it is zero vacant bits will be filled by zero and if it is one vacants bits will be filled by one.

4)now 11100000 is to be changed in hex again and it is 0XE0.

if i am wrong write me.

gautam

Lancy Mendonca

Ranch Hand

Posts: 54

sanjay gautam

Ranch Hand

Posts: 35

posted 16 years ago

hello sdev,

ans of this question will be 0XE0.

explanation----

1)first convert this number in binary , it will come out 1000001

2)after >>2 it will be 11100000, bcoz of signed right shift.

3)again change it in hex it will come out 0xE0.

i think i am right . if any doubt please write me ,

gautam

ans of this question will be 0XE0.

explanation----

1)first convert this number in binary , it will come out 1000001

2)after >>2 it will be 11100000, bcoz of signed right shift.

3)again change it in hex it will come out 0xE0.

i think i am right . if any doubt please write me ,

gautam

Tim McCauley

Greenhorn

Posts: 1

posted 16 years ago

Actually byte is 8 bits. So, by looking at the operation a cast is preformed then the shift. If the upper most bit in the byte is 1 then the number is negative. Then the shift occurs. On a shift to the right the bits are shifted off and the sign bit is shifted in. Also, the return from a shift is int if not case. So, with all of that in mind the correct answer is D 0xFFFFFFE0

Originally posted by sdev:

This below question is from the website -http://209.242.122.83/JavaCertification.html

What is the result of the following fragment of code?

(byte)0x81>>2

a. 0x20

b. 0x3FFFFFE0

c. 0XE0

d. 0xFFFFFFE0

I don't know the answer, but I want to know that, will this type of questions appear in the real exam

Thank you.......

Savithri Devaraj

Ranch Hand

Posts: 103

posted 16 years ago

Left operand of >> is always promoted to an int before the operation. Does the byte cast (byte) apply only to 0x81 or to the result after the operation.

int x = 0x81;

System.out.println((byte)x>>2) gives a result of -32.

but, System.out.println((byte)(x>>2)) gives a result of 32.

From the choices, it seems like the cast was for the result of the operation. So, the correct answer seems to be 32.

Savithri

Originally posted by sanjay gautam:

hello sdev,

ans of this question will be 0XE0.

explanation----

1)first convert this number in binary , it will come out 1000001

2)after >>2 it will be 11100000, bcoz of signed right shift.

3)again change it in hex it will come out 0xE0.

i think i am right . if any doubt please write me ,

gautam

Left operand of >> is always promoted to an int before the operation. Does the byte cast (byte) apply only to 0x81 or to the result after the operation.

int x = 0x81;

System.out.println((byte)x>>2) gives a result of -32.

but, System.out.println((byte)(x>>2)) gives a result of 32.

From the choices, it seems like the cast was for the result of the operation. So, the correct answer seems to be 32.

Savithri

daryl olson

Ranch Hand

Posts: 36

Anonymous

Ranch Hand

Posts: 18944

posted 16 years ago

This question is really tricky, I hope this kind of question is not going to appear in the real test. Who would want to shift a value of type byte in the first place?

1) cast has a higher precedence than shift operator, therefore

0x81 is cast to 10000001 (this is -1 in decimal).

2) however, the left operand of a shift operator should be of type int or long, therefore, the left operand is promoted to an integer before the shift and becomes 32 bit:

11111111 11111111 11111111 10000001 (so here -1 as a byte is promoted to a huge integer because the later is unsigned, irrational!! I don't know why we add 1 instead of 0?!), Then we do the signed shift of an unsigned integer,(again irrationally treating an unsigned number with a signed one!) and get 0xFFFFFFE0;

This question is really tricky, I hope this kind of question is not going to appear in the real test. Who would want to shift a value of type byte in the first place?

1) cast has a higher precedence than shift operator, therefore

0x81 is cast to 10000001 (this is -1 in decimal).

2) however, the left operand of a shift operator should be of type int or long, therefore, the left operand is promoted to an integer before the shift and becomes 32 bit:

11111111 11111111 11111111 10000001 (so here -1 as a byte is promoted to a huge integer because the later is unsigned, irrational!! I don't know why we add 1 instead of 0?!), Then we do the signed shift of an unsigned integer,(again irrationally treating an unsigned number with a signed one!) and get 0xFFFFFFE0;