Java Questions & Answers – Bitwise Operators

«
»

This section of our 1000+ Java MCQs focuses on Bitwise operators of Java Programming Language.

1. Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=
View Answer

Answer: d
Explanation: <= is a relational operator.
advertisement

2. Which operator is used to invert all the digits in a binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^
View Answer

Answer: a
Explanation: Unary not operator, ~, inverts all of the bits of its operand in binary representation.

3. On applying Left shift operator, <<, on integer bits are lost one they are shifted past which position bit?
a) 1
b) 32
c) 33
d) 31
View Answer

Answer: d
Explanation: The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from the right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

4. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=
View Answer

Answer: b
Explanation: None.

5. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times
b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times
c) The left shift operator can be used as an alternative to multiplying by 2
d) The right shift operator automatically fills the higher order bits with 0
View Answer

Answer: d
Explanation: The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value.
Check this: BCA MCQs | Java Books
advertisement

6. What will be the output of the following Java program?

  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 42;
  6.             int var2 = ~var1;
  7.             System.out.print(var1 + " " + var2);     	
  8.         } 
  9.     }

a) 42 42
b) 43 43
c) 42 -43
d) 42 43
View Answer

Answer: c
Explanation: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
output:

advertisement
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43

7. What will be the output of the following Java program?

advertisement
  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 3;
  6.              int b = 6;
  7.  	     int c = a | b;
  8.              int d = a & b;             
  9.              System.out.println(c + " "  + d);
  10.         } 
  11.     }

a) 7 2
b) 7 7
c) 7 5
d) 5 2
View Answer

Answer: a
Explanation: And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1.
output:

$ javac bitwise_operator.java
$ java bitwise_operator
7 2

8. What will be the output of the following Java program?

  1.     class leftshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              byte x = 64;
  6.              int i;
  7.              byte y; 
  8.              i = x << 2;
  9.              y = (byte) (x << 2)
  10.              System.out.print(i + " " + y);
  11.         } 
  12.     }

a) 0 64
b) 64 0
c) 0 256
d) 256 0
View Answer

Answer: d
Explanation: None.
output:

$ javac leftshift_operator.java
$ java leftshift_operator
256 0

9. What will be the output of the following Java program?

  1.     class rightshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x; 
  6.              x = 10;
  7.              x = x >> 1;
  8.              System.out.println(x);
  9.         } 
  10.     }

a) 10
b) 5
c) 2
d) 20
View Answer

Answer: b
Explanation: Right shift operator, >>, devides the value by 2.
output:

$ javac rightshift_operator.java
$ java rightshift_operator
5

10. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 1;
  6.              int b = 2;
  7.              int c = 3;
  8.              a |= 4;
  9.              b >>= 1;
  10.              c <<= 1;
  11.              a ^= c;
  12.              System.out.println(a + " " + b + " " + c);
  13.         } 
  14.     }

a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
View Answer

Answer: a
Explanation: None.
output:

$ javac Output.java
$ java Output
3 1 6
Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.