15 Nov Java Bitwise Operators Interview Questions
Here are Java Bitwise Operators Interview Questions. The level of questions is suitable for beginners as well advanced core Java programmers. This part covers Java Bitwise Operators and other operators.
Q71. Explain Bitwise OR operator?
A71. The | (Bitwise OR) operator will result in 1 if any of the operands has 1,
Q72. Give an example of Bitwise OR operator (|)?
A72. Here’s an example,
1 2 3 4 5 6 7 8 9 |
21 10101 28 |11100 ------- 29 11101 |
Q73. What does a Bitwise left-shift operator do?
A73. The left-shift operator (<<) shifts all the bits in a value to the left. This is done a specified number of times.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Sample { public static void main(String args[]) { byte z = 16; int x; x = z << 3; System.out.println("Value of z= " + z); System.out.println(“Value of x= " + x); } } |
Output
1 2 3 4 |
Value of z= 16 Value of x= 128 |
Q74. What does a Bitwise right-shift operator do?
A74. The right-shift operator (>>) shifts all the bits in a value to the right. This is done a specified number of times. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Sample { public static void main(String args[]) { byte z = 16; int x; x = z >> 3; System.out.println("Value of z= " + z); System.out.println(“Value of x= " + x); } } |
Output
1 2 3 4 |
Value of z= 16 Value of x= 2 |
Q75. How both Bitwise left and right shift operators can be easily differentiated?
A75. The left-shift operator multiplies a value by a specified number of times.
The right-shift operator divides a value by a specified number of times.
Q76. Give an example of compound bitwise operator assignments?
A76. For example, z >>= 7;
Q77. What are logical binary operators?
A77. The following are logical binary operators,
& = Logical AND
| = Logical OR
^ = Logical XOR
! = Logical Unary NOT
Q78. What is the role of Logical Unary NOT?
A78. Logical Unary NOT operator inverts the boolean state.
For example,
1 2 3 4 5 |
boolean z = true; boolean x = !z; System.out.println("!z = " + x); |
Output
1 2 3 |
!z = false |
Q79. What are short-circuit logical operators?
A79. The following are the two short-circuit logical operators,
|| – Short-circuit OR
&& – Short-circuit AND
Q80. Is this correct in Java?
1 2 3 4 |
int a, b, c; a = b = c = 5; |
A80. Yes, this is a chain of assignments that assigns the value of three variables in one statement.
Q81. What is a conditional (ternary) operator?
A81. It is a three-way operator that is for evaluating Boolean expressions,
Q82. Explain conditional operator with an example program?
A82. Here’s an example, that shows both the conditions,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Sample { public static void main(String args[]){ int x, y; x = 20; // the first condition is true y = (x > 5) ? 2: 10; System.out.println( "y = " + y ); // the first condition is false y = (x == 25) ? 20: 30; System.out.println( "y = " + y ); } } |
Output
1 2 3 4 |
y = 2 y = 30 |
Q83. Operators in the same row are equal in precedence? True or False?
A83. True
Q84. Why Parentheses is used?
A84. Parenthesis i.e. () are used to raise the precedence. It also makes it easy to understand the expression.
Q85. Give an example of using Parenthesis?
A85. Using parenthesis, you can make your expressions easier to read and it do not even decrease the performance of the program,
For example,
1.) 9 + z << x & 29
2.) (((9 + z) << z) & 29)
As you can see the second one is easier to read, thanks to parenthesis.
In this lesson, we saw Java Bitwise Interview Questions and Answers.
No Comments