It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes whizlabs question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "whizlabs question" Watch "whizlabs question" New topic

whizlabs question

kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298

I found this question in whizlabs mocks.

byte b = (byte) 256;


Value of b printed as _________.

Explanation of the answer is

"Since the integer's value (256) is larger than the byte's range (-128 to +127), the result is the remainder of the division of the integer value by the byte's range (256) which is zero"

I have not been able to understand the explanation of this answer.

I was thinking the solution in this way.....

00000000 00000000 00000001 00000000 (256 in Decimal)

when we cast it to byte then we are left with only the first 8 bits.


so after casting and after removing the other 24 bits, it will be 0.

I hope my method of doing it is right.

But I have not been able to understand the explanation of WHIZLABS mock.

Please someone explain that way of finding the value.


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

Your method is just fine. In addition, you can check out this SCJP Tipline article for more details.

SCJP Tipline, etc.
Abhijeet Nagre

Joined: Oct 05, 2004
Posts: 4

See 14 in binary is 1110

if you take only 1 LSB Bit the Ans is : 0
if you divide 14 BY 2(2 raise to 1) Ans is : 0

if you take only 3 LSB Bits the Ans is : 6
and if you divide 14 BY 8(2 raise to 3) Ans is : 6

So when you take LSB Bits you indirectly divide the original no
By no which is 2 raise to the no of Bits you are removing.
I agree. Here's the link:
subject: whizlabs question
It's not a secret anymore!