Test Series - java

Test Number 25/64

Q: 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
Solution: 0101 & 1111 = 0101. &= is a Bitwise AND Shorthand Assignement operator.
Q: 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
Solution: |= is a Bitwise OR Shorthand Assignment operator.
a |= b ---> a = a|b;
Q: 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
Solution: Bitwise exclusive OR produces 1 for different Inputs
and 0 for the same Inputs.
 0101
^1111
------
 1010
Q: 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
Solution: Left Shift 1 bit.
Before:
0000 0101
After:
left 0 removed <-- 0 [000 0 101 0] <--- 0 added at right
=0000 1010
Q: 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
Solution: Right Shift Operator shifts bits rightside.
Fills 0s on the left side.
Before:
0000 0101
After
[0 0000 010]1
= 0000 0010
Q: 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
Solution: 1111_1000 ==
~111 1000 + 1
= 000 0111 + 1
= 000 1000
= -8
-8 >> 1
= 1111_1100
= ~ 111 1100 + 1
= 000 0011 + 1
= 000 0100
= -4
Q: 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
Solution: In this case, >> and >>> produce the same output. A negative number is a negative number only.
Q: 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
Solution: no solution
Q: 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
Solution: (num>>1)>6
4 > 6 = false
Q: 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
Solution: Logical OR (||) is executed at last.
(4>6) || true = false||true
= true
Q: 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
Solution: Each logic block is evaluated separately as it has less priority.

You Have Score    /11