• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Practice exam question

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.......
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any takers please...........
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
sdev and hema please try following link. it will help u lot.
http://www.javaranch.com/ubb/Forum24/HTML/001250.html
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Gautam is absolutely write the correct answer is 0XE0. If it was (byte)0x81 >>> 2 then the answer would be 0x20.
 
sanjay gautam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.......


 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen this question and it's answer as defined by the author. Additionally, I ran the code (when all eles fails...)
Tim McCauley's explaination is correct.
...the correct answer is D 0xFFFFFFE0
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry about the obviously wrong statement that integer type is unsigned, I was thinking char type in my mind! sorry for the confusion, next time I'll be more careful!
Chengx
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic