Operators in Java
Short answer type questions:
1.What is an operator?
An operator is a symbol or sign used to specify an operation to be performed in Java programming.
2. Name the different types of operators.
The different types of operators are Arithmetical, Logical and Relational.
3. Explain the following:
(a) Arithmetic Operator
Arithmetic operators perform mathematical operations on numeric operands.
• Unary operators act on one operand (e.g., -a, ++a).
• Binary operators act on two operands (e.g., a + b).
Example:
int a = 10 + 20; → a = 30
(b) Relational Operator
Relational operators compare two operands and return a boolean value (true or false).
Operators: ==, !=, <, <=, >, >=
Example:
int a = 8, b = 10;
boolean c = a < b; → c = true
(c) Logical Operator
Logical operators combine multiple boolean expressions into one.
Operators: && (AND), || (OR), ! (NOT)
Example:
int a = 7, b = 10;
boolean c = a < b && a % 2 == 0; → c = false
(d) Unary Operator
Operators that act on a single operand.
Examples: +, -, ++, --
(e) New Operator
Used to create an object by dynamically allocating memory.
Example: Student s = new Student();
(f) Binary Operator
Operators that act on two operands.
Example: a + b, x > y
4. What is a Ternary Operator?
A ternary operator is a special operator that works with three operands. It provides a short way to write an
if-else statement in one line.
Syntax:
condition ? expression1 : expression2
If the condition is true, the result is expression1; otherwise, it is expression2.
Example:
int a,b,max;
a=5;
b=8;
max= a>b ? 5 : 8;
5. Differentiate between the following:
a. Binary operator and Ternary operator:
Binary operator Ternary operator
Binary operators work on two operands. Ternary operator work on three operands.
+, -, *, /, etc. are a few examples of Binary The conditional operator ? : is a Ternary operator.
operators.
b. Logical AND (&&) and Logical OR (II):
Logical AND (&&) Logical OR(||)
It evaluates to true only if both of its It evaluates to true if one or both of its operands are
operands are true. true.
Example: Example:
int a = 8, b = 13, c = 0; int a = 8, b = 13, c = 0;
if (a < 10 && b > 10) if (a > 10 || b > 10)
c = 10; c = 10;
else else
c = 5; c = 5;
Here, value of c will be 5 as one of the Here, value of c will be 10 as at least one of the
operands is false. operands is true.
c. Prefix operator and Postfix operator:
Prefix Operator Postfix Operator
It works on the principle of It works on the principle of USE-THEN-CHANGE.
CHANGE-THEN-USE.
It is written before the operand. It is written after the operand.
Example: Example:
int a = 99; int a = 99;
int b = ++a; int b = a++;
After the execution of these two After the execution of these two statements, a will
statements, both a and b will have the value of 100 and b will have the value of 99.
have the value of 100.
d. System.out.print( ) and System.out.println( )
Both methods are used to display output on the screen, but they differ in how they handle the cursor.
System.out.print( )
• Prints data and keeps the cursor on the same line.
• Next output continues on the same line.
Example:
System.out.print("Hello ");
System.out.print("World");
Output: Hello World
System.out.println( )
• Prints data and moves the cursor to the next line.
• Next output appears on a new line.
Example:
System.out.println("Hello");
System.out.println("World");
Output:
Hello
World
6. Difference between an Operator and an Expression
An operator is a symbol that performs an operation (e.g., +, -, *, /).
An expression is a combination of variables, constants, and operators that gives a result when evaluated.
Example:
• Operator: +
• Expression: a + b
7. If m=5 and n=2 then what will be the output of m and n after execution that will store in (a) & (b)?
(a) m -= n;
m -= n
⇒m=m-n
⇒m=5-2
⇒m=3
(b) n = m + m/n;
n = m + m/n
⇒n=5+5/2
⇒n=5+2
⇒n=7
8.State the difference between = and ==.
= ==
It is the assignment operator used for assigning a It is the equality operator used to check if a
value to a variable. variable is equal to another variable or literal.
E.g. int a = 10; assigns 10 to variable a. E.g. if (a == 10) checks if variable a is equal to 10 or
not.
9.What will be the output for the following program segment?
int a=0,b=10,c=40;
a = --b + c++ +b;
System.out.println(" a = " + a);
Output
a = 58
Explanation
a = --b + c++ + b
⇒ a = 9 + 40 + 9
⇒ a = 58
10. What will be the output of the following?
if x =5
(a) 5* ++x;
Answer
5 * ++x
⇒5*6
⇒ 30
(b) 5* x++;
Answer
5 * x++
⇒5*5
⇒ 25
11.Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
(a) a - (b++) * (--c);
⇒2-3*8
⇒2-3*8
⇒ 2 - 24
⇒ -22
(b) a * (++b) % c;
⇒ 2 * (4) % 9
⇒8%9
⇒8
12. If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a;
⇒ a = 5 + (5 - 10 + 6)
⇒a=5+1
⇒a=6
13. Give the output of the program snippet.
int a = 10, b =12;
if(a>=10)
a++;
else
++b;
System.out.println(" a = " + a + " and b = " +b);
Output
a = 11 and b = 12
Explanation
The condition if(a>=10) is true so a++ increments a to 11. b remains the same.
14.Rewrite the following using ternary operator.
if(income<=100000)
tax = 0;
else
tax = (0.1*income);
Answer: tax = income <= 100000 ? 0 : (0.1*income);
15. Rewrite the following using ternary operator.
if(p>5000)
d = p*5/100;
else
d = 2*p/10;
Answer: d = p > 5000 ? p * 5 / 100 : 2 * p / 10;