Java OCA OCP Practice Question 565

Question

What results from running the following code?


1. public class Main { 
2.   public static void main(String args[]) { 
3.     byte b = 10; // 00001010 binary 
4.     byte c = 15; // 00001111 binary 
5.     b = (byte)(b ^ c); 
6.     System.out.println("b contains " + b); 
7.   } 
8. } 
  • A. The output: b contains 10
  • B. The output: b contains 5
  • C. The output: b contains 250
  • D. The output: b contains 245


B.

Note

The XOR operator produces a 1 bit in any position where the operand bits are different.

So 00001010 ^ 00001111 is 00000101, which is 5.




PreviousNext

Related