Java Interview Questions With Answers – Part 6: Bitwise Operators

[1][2][3][4][5][6] – Here’s part 6 of some Java 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.

Java-Bitwise-Operators-Interview

The series includes more than 200 questions, so stay tuned for more.

Q71. Explain Bitwise OR operator?

A71.  The | (Bitwise OR) operator will result in 1 if any of the operand has 1,

Q72. Give an example of Bitwise OR operator (|)?

A72.  Here’s an example,

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 for a specified number of times. For example,

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

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 for a specified number of times. For example,

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

Value of z= 16

Value of x=  2

Q75. How both Bitwise left and right shift operators can be easily differentiated?

A75. Left shift operator multiplies a value by specified number of times.

Right shift operator divides a value by 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. & = 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,

boolean z = true;
boolean x = !z;
System.out.println(“!z = ” + x);

Output

!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?

int a, b, c;

a = b = c = 5;

A80. Yes, this is 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,

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

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.

[1][2][3][4][5][6]

thegenericwhiz@gmail.com'
Editorial Staff at Generic Whiz.

Comments

comments

thegenericwhiz@gmail.com'

GW Editorial Staff

Editorial Staff at Generic Whiz.

Leave a Reply

Your email address will not be published. Required fields are marked *