50+ Java Operators Interview Questions Answers

Here, we have listed the most important topic-wise Java operators interview questions with answers that are for freshers and experienced.

These operators interview questions are often asked in any technical tests and interviews.

We have also covered interview coding programming questions based on types of operators in Java that are asked to solve in the technical tests.

So, you must prepare answers to these operators interview questions.

Java Operators Interview Questions and Answers


1. What is an Operator in Java?

Ans: A operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is generally used in a program to perform a particular function on operands.

2. What are the types of operators based on the number of operands?

Ans: There are three types of operators in java based on the number of operands. They are:

  • Unary operator
  • Binary operator
  • Ternary operator

3. What is Unary operator in Java?

Ans: A operator that acts on a single operand is called unary operator. It uses a single variable.

4. What is a Binary operator in Java?

Ans: A operator that acts on two operands is called binary operator. It uses two variables.

5. What is Ternary operator in Java?

Ans: A operator that acts on three operands is called ternary operator. It uses three variables.

6. What is Expression in Java?

Ans: An expression in java is a combination of operators and operands that is used to perform mathematical or logical calculations. In simple words, it is a combination of variables, constants, and operators.

For example, an expression is x+5. Here, the operand x is a variable, operand 5 is a constant, and + is an operator that acts on these two operands and produces the desired result.

7. What is a symbolic operator in Java? What are the types of operators based on symbols?

Ans: If a symbol like +, -, *, etc is used as an operator, it is called symbolic operator.

There are mainly eight types of operators based on symbols. They are as follows:

  • Arithmetic operators ⇒ +, -, *, /, etc.
  • Relational operators ⇒ <, >, <=, >=, = =, !=.
  • Logical operators ⇒ &&, ||, !.
  • Assignment operators ⇒ =,
  • Increment and decrement operators ⇒ + +, – –
  • Conditional operators ⇒ ?:
  • Bitwise operators ⇒ &, !, ^, ~, <<, >>, >>>
  • Shift operators ⇒ <<, >>, >>>.

8. What is named operator in Java?

Ans: If a keyword is used as an operator, it is called named operator. The named operator is instanceof.

Interview Questions based Arithmetic Operator in Java


1. What is Arithmetic operators in Java?

Ans: Operators that are used to performing fundamental arithmetic operations such as addition, subtraction, multiplication, and division on numeric data types are called arithmetic operators.

2. What are the types of arithmetic operators?

Ans: Java supports five types of arithmetic operators. They are:

  • + Addition
  • – Subtraction
  • * Multiplication
  • / Division
  • % Modulo division (Remainder)

3. What are the priority levels of arithmetic operation in Java?


Ans: There are two priority levels of arithmetic operation in java. They are as follows:

  • High priority ⇒ * / %
  • Low priority ⇒ + –

4. What will be the output of the following program?

a)

public class Test {
public static void main(String[] args)
{
  int x = 2, y = 5;
 
  int exp1 = (x * y / x);
  int exp2 = (x * (y / x));
 
  System.out.println(exp1);
  System.out.println(exp2);
  }
}

Ans: Output is 5, 4.

b)

public class Test {
public static void main(String[] args)
{
 int x = 10, y = 5;
 
 int exp1 = (y * (x / y + x / y));
 int exp2 = (y * x / y + y * x / y);
 
 System.out.println(exp1);
 System.out.println(exp2);
  }
}

Ans: Output is 20, 20.

5. What is the result of the following code?

public class Test {
public static void main(String[] args)
{
  double exp1 = (3.0 * 2 / 4);
  int exp2 = (3 * -2 % 4);
 
  System.out.println(exp1);
  System.out.println(exp2);
  }
}

Ans: Results are 1.5, -2.

6. What is the output of following code?

public class Test {
public static void main(String[] args)
{
  int x = 9, y = 12, z = 3;
 
  int exp1 = x - y/3 + z * 2 - 1; 
  int exp2 = (x - y)/3 + ((z * 2) - 1); 
 
  System.out.println(exp1);
  System.out.println(exp2);
  }
}

Ans: Outputs are 10, 4.


7. Is the arithmetic expression valid? If yes, what is the output of the following expression?

public class Test {
public static void main(String[] args)
{
  int x = 9, y = 12;
  int a = 2, b = 4, c = 6;
 
   int exp = (3 + 4 * x)/5 - 10 * (y - 5) * (a + b + c)/x + 9 * (4/x + (9 + x)/y);
 
   System.out.println(exp);
  }
}

Ans: Yes, the arithmetic expression is valid. The output is -77.

8. What would be the result of the following arithmetic expression?

public class Test {
public static void main(String[] args)
{
   int x = 9, y = 12;
   int a = 2, b = 4, c = 6;
 
   int exp = 4/3 * (x + 34) + 9 * (a + b * c) + (3 + y * (2 + a)) / (a + b*y);
 
   System.out.println(exp);
  }
}

Ans: 278

For explanation of answer, follow this tutorial: Java Operators | Arithmetic Operators Example

Interview Questions based on Relational Operators


1. What are Relational operators in Java?

Ans: Relational operators in Java are those operators that are used to perform the comparison between two numeric values or two quantities. They are generally used in looping and branching statements to create conditions.

2. How many types of relational operators are supported by Java?

Ans: Java supports six types of relational operators. They are as follows:

  • < Less than
  • <= Less than or equal to
  • > Greater than
  • >= Greater than or equal to
  • == Equal to
  • != Not equal to

3. What is the result of relational operator when it is used in looping or branching statement?

Ans: The result of all relational operators is always of a boolean type. It returns always true or false.

4. What will be the output of the following program?

public class Test {
public static void main(String[] args)
{
  int x = 9, y = 12;
  int a = 2, b = 4;
 
  boolean exp = 4/3 * (x + 34) < 9 * (3 + y * (2 + a)) / (a + b*y);
  System.out.println(exp);
  }
}

Ans: false

Interview Questions based on Logical operators


1. What are Logical operators in Java?

Ans: Logical operators in Java are those operators that are used to form compound conditions by combining two or more conditions or relational expressions.

These operators are also called Boolean operators because they return a boolean value.

2. How many types of logical operators are supported by Java?

Ans: Java supports three types of logical operators. They are as follows:

  • && AND operator
  • || OR operator
  • ! NOT operator

3. What will be the output of the following program?

public class Test { 
public static void main(String[] args) 
{ 
  int x = 200, y = 50, z = 100; 
  if(x > y && y > z)
 { 
     System.out.println("Hello"); 
  } 
 if(z > y && z < x)
{ 
   System.out.println("Java"); 
} 
 if((y+200) < x && (y+150) < z)
{ 
   System.out.println("Hello Java"); 
 } 
} 
}

Ans: Output: Java

4. What will be the output of the following code?

public class Test{ 
public static void main(String[] args) 
{ 
  int x = 1, y = 2, z = 5; 
if(x == 1 || x > y || x > z)
{ 
    System.out.println("One"); 
} 
if(x == y || y == 2 || z == 5)
{ 
   System.out.println("Two"); 
} 
if(x == y || y == z || z == x) 
{ 
    System.out.println("Three"); 
  } 
 } 
}

Ans: Output: One, Two

5. Will the following code compile successfully? If yes, What will be the output of the program?

public class Test { 
public static void main(String[] args) 
{ 
  int x = 1, y = 2, z = 5; 
  System.out.println("x: " +(!((x + 2) == (1 + 2)))); 

  System.out.println("y: " +(!(y == z))); 
  System.out.println("z>x: " +(!(z > x))); 

  if(!(x == y) && ((y + 5) > z) && (!((z - 3) == 0)))
  { 
      System.out.println("Hello"); 
   } 
 } 
}

Ans: Output: x: false, y: true, z>x: false, Hello.

For explanation, go to this tutorial: Logical operators in Java

Interview Questions Based on Assignment Operators


1. What is an Assignment operator in Java?

Ans: An operator which is used to store a value into a particular variable is called assignment operator in java.

2. Is there any difference between x += y and x = x + y expressions?

Ans: No, there is no any difference between x += y and x = x + y expressions. Both are equivalent to each other.

3. What will be the result of the following code?

public class Test { 
 public static void main(String[] args) 
{ 
   int x = 20, y = 30, z = 50; 
    x += y; 
    y -= x + z; 
    z *= x * y;
 System.out.println("x = " +x ); 
 System.out.println("y = " +y ); 
 System.out.println("z = " +z ); 
 } 
}

Ans: Output: x = 50, y = -70, z = -175000

4. Identify the errors in the following code.

public class Test {
public static void main(String[] args)
{
  int x = y = z = 10;
  System.out.println(x + " " + y + " " + z);
  }
}

Ans: Compile time error because we cannot assign a variable to variable. For example, int x = y;

5. Will the code compile successfully? If yes, what will be the output of code?

public class Test {
public static void main(String[] args)
{
   int x, y, z;
   x = y = z = 2;
   x += y;
   y -= z;
   z /= (x + y);
 System.out.println(x + " " + y + " " + z);
  }
}

Ans: Yes, the code will be compiled successfully. No error. The output is 4, 0, 0.

6. What will be the output of code if no error?

public class Test {
public static void main(String[] args)
{
  int x, y, z;
  1 = x;
  y = z = 2;
 
  int a = x + y + z;
  System.out.println(x + " " + y + " " + z);
  }
}

Ans: Compile time error because the left-hand side of an assignment must be a variable.