Please open the menu to show more

Tuesday, August 15, 2017

Frequently asked interview question on java programming language part seven (7)

Question: What will be the output of this code, also explain your answer.

class Test
{
public static void main(String[] args)
{
for(byte b=1;b<=127;b++)
{
System.out.println(b);
}
}
}

Output of code =>
 This loop will print numbers from 1 to 127 then -128 to 0, then it will do it
                  the same for infinite times, so it is an infinite loop

Explanation of code =>
                       ## The range of byte datatype is -128 to 127
      ## So any value greater than 127 will round off to -128 and
                       any value less than -128 will round off to 127 due to
                       most significant bit (MSB)
      ## The MSB is zero for positive value and 1 for negative values
   

No comments:

Post a Comment