Java OCA OCP Practice Question 802

Question

What happens when you try to compile and run the following class...

public class Main{ 
      public static void main (String [] args) throws Exception{ 
            int a = Integer.MIN_VALUE; 
            int b = -a; 
            System .out.println ( a+ "   "+b); 
       } 
} 

Select 1 option

  • A. It throws an StackOverFlowError.
  • B. It will print two same negative numbers.
  • C. It will print two different negative numbers.
  • D. It will print one negative and one positive number of same magnitude.
  • E. It will print one negative and one positive number of different magnitude.


Correct Option is  : B

Note

It prints: -2147483648 -2147483648

Negative integers are stored in 2's complement form (complement the bits and add 1).

For example: Integer 1 in binary is 00000000 00000000 00000000 00000001 (32 bits)

So - 1 in binary would be (complement the bits for 1 and add 1) :

  • Step 1 (complement the bits of 1): 11111111 11111111 11111111 11111110
  • Step 2 (add 1 to step 1): 11111111 11111111 11111111 11111111.

Now, let's see what happens in this question:

a = Integer.MIN_VALUE = 10000000 00000000 00000000 00000000 

To get -a, apply the above two steps:

  • Step 1 (complement the bits): 011111111 11111111 11111111 11111111
  • Step 2 (add 1) : 10000000 00000000 00000000 00000000

So you got the exact same value that you started with!

You can see the binary form of an integer using Integer.toBinaryString (i) method.




PreviousNext

Related