Java OCA OCP Practice Question 765

Question

What happens when you try to compile and run the following program?

public class Main{ 
   public static void main (String args [] ){ 
      byte b = -128 ; 
      int i = b ; 
      b =  (byte) i; 
      System.out.println (i+" "+b); 
    } 
 } 

Select 1 option

  • A. The compiler will refuse to compile it because i and b are of different types cannot be assigned to each other.
  • B. The program will compile and will print - 128 and - 128 when run .
  • C. The compiler will refuse to compile it because - 128 is outside the legal range of values for a byte.
  • D. The program will compile and will print 128 and - 128 when run .
  • E. The program will compile and will print 255 and - 128 when run .


Correct Option is  : B

Note

byte and int both hold signed values. So when b is assigned to i, the sign is preserved.




PreviousNext

Related