Questions & Answers on Operators

Operator questions and answers will help you to clear concept and increase your knowledge on the previously discussed chapter. These questions can be asked in any interviews, college vivas etc. I hope you will learn something out of these questions and answers.

  1. What are the operators present in Java?

The operators present in Java are:

  • Unary
  • Binary: logical, shift, relational, assignment, arithmetic, bitwise
  • Ternary

2. What is type casting and why do we use explicit type casting in Java?

A cast is a way of explicitly informing the compiler that we intend to make the conversion and we are aware that data loss might occur. To perform a cast, specify the type that you casting to in parentheses in front of the value or variable to be converted.

Explicit typecasting is done so that during runtime a class cast Exception does not occur, which occurs if the object being cast is not compatible with the new type it is being cast to.

3. What is the instanceOf Operator?

This operator always checks whether the object belongs to the same class or its child class and returns a boolean value.

4. According to Java operator precedence, which operator is considered to be with the highest precedence?

[ ] which means accessing an array/array Index.

5. Which Java operators are right associative?

Conditional, operation and assignment, pre or postfix increasing order, pre or postfix descending, Unary plus and minus, bitwise NOT, boolean NOT, typecast, object creation.

6. What's the difference between >> and >>>?

>> is signed bit right shift.
>>> is unsigned bit right shift.

7. What is dot operator?

The dot operator is used to access the variable, method, constant, object, package, class.

8. What's the difference between = and ==?

= is an assignment operator which assigns a value to other variables.

== is a relational operator which checks whether a given value is equal to the value given on the right side of the operator.

9. What is the difference between double and float variable in java?

The float data type is a single precision 32-bit floating point. Its default value is 0.0f and wrapper class is Float class.

The double data type is a double precision 64-bit floating point. Default value is 0.0d. The wrapper class is double class. Real constant nature is always double.

10. What's ternary operator, give example?

This operator is used to check greatest between 2 numbers.

Ex: int res=10>2?10:2



11. How to convert a byte stream to character stream and vice-versa?

First, Byte -›String
byte x=10;
Byte.toString(x);

then, String -›char

Character.parseChar("10");

The vice-versa can also be done:

Char -›String

Character.toString('A')

then, String -›byte

Byte.parseByte("65");

12. What is the % operator's use?

It is used to find out the remainder between 2 numbers.

13. Is &&= a valid Java operator?

No.