Q: What is the output of the Java program? byte b = 0b0000101; System.out.print(b + ","); b &= 0b00001111; System.out.print(b);
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);
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);
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);
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);
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);
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);
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);
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); }
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); }
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); }
Solution: Each logic block is evaluated separately as it has less priority.
You Have Score    | /11 |