Java MCQ Questions and Answers on Bitwise Operators 2

Study and learn Java MCQ Questions and Answers on Bitwise Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Bitwise Operators before reading these objective questions.



1) What is the output of the Java program?
byte b = 0b0000101;
System.out.print(b + ",");
b &= 0b00001111;
System.out.print(b);

 
A) 5, 0
B) 5,5
C) 5,15
D) None
Answer [=]
B
Explanation:

0101 & 1111 = 0101. &= is a Bitwise AND Shorthand Assignement operator.

2) What is the output of the Java program?
byte b = 0b00000101;
System.out.print(b + ",");
b |= 0b00001111;
System.out.print(b);
A) 5,0
B) 5,5
C) 5,15
D) Compiler error
Answer [=]
C
Explanation:
|= is a Bitwise OR Shorthand Assignment operator.
a |= b ---> a = a|b;
3) What is the output of the Java Program?
byte b = 0b00000101;
System.out.print(b + ",");
b ^= 0b00001111;
System.out.print(b);
A) 5,0
B) 5,5
C) 5,10
D) 5,15
Answer [=]
C
Explanation:
Bitwise exclusive OR produces 1 for different Inputs
and 0 for the same Inputs.
 0101
^1111
------
 1010
4) What is the output of the Java program?
byte b = 0b00000101;
System.out.print(b + ",");
b = (byte)~b;
System.out.print(b);
A) 5, 10
B) 5, -10
C) 5. -6
D) Compiler error
Answer [=]
C
Explanation:
Bitwise NOT(~) operator turns 1s to 0s
and 0s to 1s. If the leftmost bit is 1,
it is a negative number.
So take 1's complement and add 1 to it.
~0000 0101 =
 1111 1010
-----------
~ 111 1010 =
  000 0101
-----------
  000 0101
  000 0001 +
-----------
- 000 0110
5) What is the output of the Java program with Left Shift Operator (<<)?
byte b = 0b00000101;
System.out.print(b + ",");
b = (byte)(b << 1);
System.out.print(b);
A) 5,2
B) 5,5
C) 5,10
D) Compiler error
Answer [=]
C
Explanation:
Left Shift 1 bit.
Before:
0000 0101
After:
left 0 removed <-- 0 [000 0 101 0] <--- 0 added at right
=0000 1010
6) What is the output of Java program with Right Shift Operator (>>)?
byte b = 0b00000101;
System.out.print(b + ",");
b = (byte)(b >> 1);
System.out.print(b);
A) 5,-6
B) 5,10
C) 5, 2
D) Compiler error
Answer [=]
C
Explanation:
Right Shift Operator shifts bits rightside.
Fills 0s on the left side.
Before:
0000 0101
After
[0 0000 010]1
= 0000 0010

 
7) What is the output of the Java program with Right Shift (Without Fill Zeroes)?
byte num = (byte)0b1111_1000;
System.out.print(num + ",");
num = (byte)(num >> 1);
System.out.print(num);
A) -8, -16
B) -4, -8
C) -8, -4
D) Compiler error
Answer [=]
C
Explanation:
1111_1000 ==
~111 1000 + 1
= 000 0111 + 1
= 000 1000
= -8
-8 >> 1
= 1111_1100
= ~ 111 1100 + 1
= 000 0011 + 1
= 000 0100
= -4


8) What is the output of a Java Program with Shift Right Fill Zeroes (>>>) operator?
byte num = (byte)0b1111_1000;
System.out.print(num + ",");
num = (byte)(num >>> 1);
System.out.print(num);
A) -8, 8
B) -8, 4
C) -8, -4
D) Compiler error
Answer [=]
C
Explanation:

In this case, >> and >>> produce the same output. A negative number is a negative number only.

9) What is the output of Java Program with Shift Right Fill Zeroes operator?
byte num = (byte)0b000_1000;
System.out.print(num + ",");
num = (byte)(num >>> 1);
System.out.print(num);
A) -8, 4
B) 8, 4
C) 8, -4
D) -8, -4
Answer [=]
B
10) What is the output of the Java program?
byte num = (byte)0b000_1000;
if(num >> 1 > 6)
{
 System.out.print(num);
}
else
{
 System.out.println(num>>1);
}
A) 8
B) 6
C) 4
D) Compiler error
Answer [=]
C
Explanation:
(num>>1)>6
4 > 6 = false
11) What is the output of the Java Program?
byte num = (byte)0b000_1000;
if(num >> 1 > 6 || true)
{
System.out.print(num);
}
else
{
System.out.println(num>>1);
}
A) 8
B) 6
C) 4
D) 2
Answer [=]
A
Explanation:
Logical OR (||) is executed at last.
(4>6) || true = false||true
= true
12) What is the output of the Java program?
byte num = (byte)0b000_1000;
if(num >> 1 > 6 || true | 2>3)
{
System.out.print(num+1);
}
else
{
System.out.println(num>>2);
}
A) 8
B) 6
C) 9
D) Compiler error
Answer [=]
C
Explanation:

Each logic block is evaluated separately as it has less priority.