Link Search Menu Expand Document

TOP-5 Java Operators Interview Questions

In this article, we will be discussing some important interview questions related to Java operators.

1. What is the Java ternary operator?

Answer:

The ternary operator is also known as a conditional operator. It is a short-cut operator for the if-else statement. It has the following syntax:

variable = (condition)?value1:value2;

It evaluates the specified condition. If the condition is true, it assigns value1 to variable, otherwise, it assigns value2.

The following code snippet demonstrates this operator:

int marks = 85;
char grade = marks>80?'A':'B';
System.out.println("grade is:"+grade);

This code declares and initializes a variable called marks. It uses the ternary operator to check if marks>80. Since marks have the value 85, ‘A’ is assigned to grade. So, this code displays the following output:

grade is:A

2. What is the difference between the prefix and postfix increment/decrement operators?

Answer:

The increment/decrement operator is said to be in prefix form when the operator is placed before the operand and in postfix form when the operator is placed after the operand. Their behavior differs when they are used in an assignment. When the prefix form is used, the increment/decrement operation is performed before the assignment. When the postfix form is used, the increment/decrement operation is performed after the assignment.

Consider the following code:

int value1=6;
int value2=++value1;
System.out.println("Prefix:");
System.out.println("value1="+value1);
System.out.println("value2="+value2);

value1 = 9;
value2 = value1++;
System.out.println("Postfix:");
System.out.println("value1="+value1);
System.out.println("value2="+value2);

Line 1 declares a variable called value1 and initializes it to 6. Line 2 uses the increment operator in prefix form on value1 and assigns the result to value2. Since the prefix form is used, value1 is first incremented and the increment value is assigned to value2. Next, Line 7 uses the increment operator in postfix form. In this case, value1 is first assigned to value2 and then value1 is incremented.

This code produces the following output:

Prefix: value1=7 value2=7 Postfix: value1=10 value2=9

3. What is the Java modulus operator? Explain with a code sample

Answer:

The Java modulus operator is represented by the % sign. It returns the remainder of the division.

The following code demonstrates this:

int input = 27;
int output = input%9;
System.out.println(output);

In this case, the modulus is used on the variable input to obtain the remainder after dividing input (27) by 9. Since 27 is divisible by 9, the module operator returns a 0 which gets printed to the console.

4. Explain the difference between the Bitwise OR operator and the Logical OR operator

Answer:

The following are the differences between the Bitwise OR operator and the Logical OR operator:

  1. The Bitwise OR operator is represented by the “|” symbol while the logical OR operator is represented by the “||” symbol
  2. The Bitwise OR operator operates on the binary representation of numeric values while the logical OR operator operates on boolean values
  3. The Bitwise OR operator evaluates both arguments while the Logical OR operator evaluates the second argument only if the first argument is false.

5. Explain the Java shift Right shift operator

Answer:

The Java right shift operator is represented by >> sign. It is a bitwise operator, that is it operates on the binary representation of an integer. It shifts all the bits in a number to the right by a specified number of places. It has the following form:

Value_to_shift>>num_of_places

This indicates that the bits in value_to_shift should be shifted to the right by num_of_places places.

Other useful articles:


Back to top

© , Java Interviews — All Rights Reserved - Terms of Use - Privacy Policy