#### Topics:- **Operators in Java**

**Que 1. What are Operators?**

**Answer:** Operators are special symbols in a programming language and perform certain specific operations.

**Que 2. What do you mean by Arithmetic Operators?**

**Answer:** Operators which perform the basic arithmetic operations – add, sub, mul, div, rem, are known as Arithmetic operators.

**Que 3. Explain all types of arithmetic Operators with suitable examples.**

**Answer:** Arithmetic operators are –

Operator | Description | Explanation with Example |

+ | Addition | Returns the sum of values of operands. int a = 30; int b = 20; int c = a + b; System.out.println(c); Output is: 50 |

– | Subtraction | Returns the difference of values of operands. int a = 30; int b = 20; int c = a – b; System.out.println(c); Output is: 10 |

* | Multiplication | Returns the product of values of operands. int a = 30; int b = 20; int c = a * b; System.out.println(c); Output is: 600 |

/ | Division | Returns the quotient of division of values of operands. int a = 30; int b = 20; int c = a / b; System.out.println(c); Output is: 1 |

% | Modulus | Returns the remainder of division of values of operands. int a = 30; int b = 20; int c = a % b; System.out.println(c); Output is: 10 |

++ | Increment | Pre Increment (++x): First Increase the value of operands by 1, then use the value of operand or variable. [Increase then use] int a = 30; int b = ++a; System.out.println(“a =” +a); System.out.println(“b =” +b); Output is: a = 31 b = 31 Post Increment (x++): First use the value of operand then Increase the value of operands by 1. [Use then Increase] int a = 30; int b = a++; System.out.println(“a =” +a); System.out.println(“b =” +b); Output is: a = 31 b = 30 |

— | Decrement | Pre Decrement (–x): First decrease the value of operands by 1, then use the value of operand or variable. [Decrease then use] int a = 30; int b = –a; System.out.println(“a =” +a); System.out.println(“b =” +b); Output is: a = 29 b = 29 Post Decrement (x–): First use the value of operand then Decrease the value of operands by 1. [Use then Decrease] int a = 30; int b = a–; System.out.println(“a =” +a); System.out.println(“b =” +b); Output is: a = 29 b = 30 |

**Que 4. What do you mean by Relational Operators?**

**Answer:** Relational operators are used for comparing two similar types of values. It returns a Boolean value either true or false.

**Que 5. Explain all types of Relational Operators with suitable example.**

**Answer:** Relational operators are –

Operator | Description | Explanation with Example |

== | Equal to | Returns true if values of operands are equal, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a == b); System.out.println(a == 10); } } Output is: false true |

!= | Not Equal to | Returns true if values of operands are not equal, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a != b); System.out.println(a != 10); } } Output is: true false |

> | Greater than | Returns true if the values of operand1 are greater than operand2, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a > b); System.out.println(b > a); } } Output is: false true |

< | Less than | Returns true if the values of operand1 are less than operand2, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a < b); System.out.println(b < a); } } Output is: true false |

>= | Greater than or equal to | Returns true if values of operand1 are greater than or equal to operand2, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a >= b); System.out.println(b >= 20); } } Output is: false true |

<= | Less than or equal to | Returns true if the values of operand1 are less than or equal to operand2, false otherwise. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a <= b); System.out.println(b <= 20); } } Output is: true true |

**Que 6. What is the assignment operator?**

**Answer:** Assignment operator = is used to assigning a value to the variable.

variable = value;

num = 250 ;

**Que 7. What is the arithmetic assignment operators?**

**Answer:** A combination of arithmetic and assignment operators is called the arithmetic assignment operator. e.g +=, *=, -=, /=, %=,

**Que 8. Explain all types of Assignment Operators.**

**Answer:** Assignment operators are –

Operator | Description | Explanation with Example |

= | Simple Assignment | Assigns value of left side operand to right side operand. class RelationalOperator { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(“a =”, +a); System.out.println(“b =” +b); } } Output is: a = 10 b = 20 |

+= | Add and Assignment | Adds value of left side operand to right side operand and assigns the result (sum) to the left side operand. Same as a = a+b => a += b int a = 30; System.out.println(“a =”, a); a += 20;System.out.println(“a =”, a); Output is: a = 30 a = 50 |

-= | Subtract and Assignment | Subtracts value of left side operand from right side operand and assigns the result (difference) to the left side operand. Same as a = a – b => a -= b int a = 30; System.out.println(“a =”, a); a -= 20; System.out.println(“a =”, a); Output is: a = 30 a = 10 |

*= | Multiply and Assignment | Multiplies value of left side operand to right side operand and assigns the result (product) to the left side operand. Same as a = a * b => a *= b int a = 30; System.out.println(“a =”, a); a *= 20; System.out.println(“a =”, a); Output is: a = 30 a = 600 |

/= | Divide and Assignment | Divides value of left side operand by right side operand and assigns the result (quotient) to the left side operand. Same as a = a / b => a /= b int a = 30; System.out.println(“a =”, a); a /= 20;System.out.println(“a =”, a); Output is: a = 30 a = 1 |

%= | Modulus and Assignment | Divides value of left side operand by right side operand and assigns the result (remainder) to the left side operand. Same as a = a % b => a %= b int a = 30; System.out.println(“a =”, a); a %= 20; System.out.println(“a =”, a); Output is: a = 30 a = 10 |

**Que 9. What is the logical operator?**

**Answer:** Logical operators are used for manipulating or handling the logical values i.e. True and False.

**Que 10. Explain all types of Logical Operators.**

**Answer:** Logical operators are –

Operator | Description | Explanation with Example |

&& | Logical AND | Returns true if values of both a and b are true, false otherwise class LogicalOperator { public static void main(String[] args) { int a = 10; int b = 20; boolean result1 = (a > b && b > a); // false and true => false boolean result2 = (a < b && b > a); // true and true => true System.out.println(“(a > b && b > a) =>”+result1); System.out.println(); System.out.println(“(a < b && b > a) =>”+result2); } } Output is: (a > b && b > a) =>false (a < b && b > a) =>true |

|| | Logical OR | Returns true if values of both a and b are true, false otherwise class LogicalOperator { public static void main(String[] args) { int a = 10; int b = 20; boolean result1 = (a > b || b < a); // false or false => false boolean result2 = (a < b || b < a); // true or false => true System.out.println(“(a > b && b < a) =>”+result1); System.out.println(); System.out.println(“(a < b && b < a) =>”+result2); } } Output is: (a > b && b < a) => false (a < b && b < a) =>true |

! | Logical NOT | Returns true if value is false, false otherwise. class LogicalOperator { public static void main(String[] args) { int a = 10; int b = 20; boolean result1 = !(a > b || b < a); // NOT(false or false) => true boolean result2 = !(a < b || b < a); // NOT(true or false) => false System.out.println(“!(a > b || b < a) =>” + result1); System.out.println(); System.out.println(“!(a < b || b < a) =>”+result2); } } Output is: !(a > b && b > a) =>true !(a < b && b > a) =>false |

**Que 11. Which java operator is used for concatenation?**

**Answer:** + (concat operator)

**Que 12. Differentiate between / and % operators?**

**Answer:** / & % both operators come under the category of Arithmetic operator.

**Division Operator (/ ) **known as the division operator, which returns the quotient value when you divide one number by another number.

**Modulus Operator (% )**is known as the modulus operator, which returns the remainder value when you divide one integer number by another integer number.

**Que 13. Predict the output of the following expression of the initial value of x is 5.**

x=( (++x) * 2 ) + 7 => 6 * 2 + 7 => 19

a. 19 b. 21 c. 18 d. 20

**Answer:** a. 19

**Que 14. Predict the Output:**

int x =9, k =0;

int y = 10;

y = ++x;

x += y;

k = x;

What will be the value of k?

**Answer**: 20

**Que 15. Identify the type of operator used:**

If (A >=B)

(a) Arithmetic (b) Relational (c) Logical (d) Assignment

**Answer:** Relational

**Que 16. Differentiate between = and == operator in JAVA.**

**Answer: ** = is an assignment operator == is a equality comparison operator

For example:

int a =10; Will assign a value 10 to variable a

Whereas a==10 will check whether the value of a is 10

**Que 17. Write a JAVA program (method) to develop an application to accept sales of a company for four quarters. Calculate the total yearly sale and display the same. [SQP]**

**Answer**:

float q1, q2, q3, q4, total;

q1=Float.parseFloat(jTextField1.getText());

q2=Float.parseFloat(jTextField2.getText());

q3=Float.parseFloat(jTextField3.getText());

q4=Float.parseFloat(jTextField4.getText());

total = q1+q2+q3+q4;

jTextField5.setText(Float.toString(total));

**Que 18. Write a JAVA program (method) to develop an application in JAVA to calculate Marks in 5 subjects and show the percentage of Students**. [SQP]

**Answer:**

float m1,m2,m3,m4,m5,total,perc;

m1=Float.parseFloat(jTextField1.getText());

m2=Float.parseFloat(jTextField2.getText());

m3=Float.parseFloat(jTextField3.getText());

m4=Float.parseFloat(jTextField4.getText());

m5=Float.parseFloat(jTextField5.getText());

total = m1+m2+m3+m4+m5;

perc=(total/500) *100;

jTextField6.setText(Float.toString(perc));

**Que 19. Write a program in Java to display Area of a square**.

**Answer:**

static double square area (double side) {

return (side * side);

}

OR

int side, area;

side = Integer.parseInt(jTextField1.getText());

area = side * side;

jTextField2.setText(Integer.toString(area));

**Que 20. If a = 20 and b = 30 What will be the value of a % = b?**

**Answer: **The value of a is 20.

**Que 21. If a = 30 and b = 20 What will be the value of a % = b?**

**Answer: **The value of a is 10. (remainder value)

**Que 22. Write a JAVA program (method) to develop an application in JAVA to calculate area and perimeter of circle**. [SQP]

**Answer:**

float radius, area , perimeter;

radius = Float.parseFloat(jTextField1.getText());

area = 3.14 * radius * radius;

perimeter = 2 * 3.14 * radius ;

jTextField2.setText(Float.toString(area));

jTextField3.setText(Float.toString(perimeter));

**Que 23. Write a JAVA program (method) to develop an application in JAVA to calculate Total Amount after inputting the price and quantity**. [CBSE QP]

**Answer:**

float price, quantity , total_amount;

price = Float.parseFloat(jTextField1.getText());

quantity = Float.parseFloat(jTextField2.getText());

total_amount = price * qunatity;

jTextField3.setText(Float.toString(total_amount));

**Que 24. Write a JAVA program (method) to develop an application in JAVA to calculate area of right angle triangle**. [CBSE QP]

**Answer:**

float base, height, area;

base = Float.parseFloat(jTextField1.getText());

height = Float.parseFloat(jTextField2.getText());

area = (base * height) / 2;

jTextField3.setText(Float.toString(area));

**Que 25. Write a JAVA program (method) to develop an application to accept sales of a company for four quarters. Calculate the total yearly sale and display the same**. [CBSE SQP]

**Answer:**

float q1, q2, q3, q4, total_sale;

q1=Float.parseFloat(jTextField1.getText());

q2=Float.parseFloat(jTextField2.getText());

q3=Float.parseFloat(jTextField3.getText());

q4=Float.parseFloat(jTextField4.getText());

total_sale = q1 + q2 + q3 + q4;

jTextField5.setText(Float.toString(total_sale));

**Que 26. Which of the following is not a comparison operator ? [CBSE 2017]**

(a) <> (b) < (c) < = (d) > =

**Answer:** (a) <>

**Que 27. With x = 0, which of the following are legal lines of Java code for changing the**

**value of x to 1? [****CBSE 2017 COMPT****]**

1. x ++;

2. x = x + 1;

3. x += 1;

4. x =+ 1;

(a) 1, 2 & 3

(b) 1 & 4

(c) 1, 2, 3 & 4

(d) 3 & 2

**Answer:** (a) 1, 2 & 3

**Que 28. Which of these operators can be used to concatenate two or more String objects? ****[CBSE 2017 COMPT]**

(a) +

(b) +=

(c) &

(d) ||

**Answer:** (a) +

**Que 29. If x = 20, y = 10, what will be the value of x, if we write the statement**

**x += y ? [CBSE 2019]**

(a) 30

(b) 20

(c) 10

(d) 21

**Answer:** (a) 30

**Que 30. What is the output of relational operators? [CBSE 2019]**

(a) Integer

(b) Boolean

(c) Characters

(d) Double

**Answer:** (b) Boolean

**Que 31. Consider the following statements: [****CBSE 2019 COMPT****]**

A = 10; //first statement

A == 10; //second statement

Is there any difference between the two statements? Justify your answer.

**Answer:** Yes

First statement is an assignment statement, which assign a value 10 to the variable A, whereas the second statement is the comparison statement, which compare the value of A with 10 and returns a Boolean value either true or false.

**Que 32. What will be the output of the following code and why?**

public class first

{

public static void main(String[] args)

{

System.out.println(2 + 0 + 1 + 8 + “Welcome”); //Statement 1

System.out.println(“Welcome” + 2 + 0 + 1 + 8); //Statement 2

}

}

**Answer: **Output is

11Welcome

Welcome2018

**Reason:**

**System.out.pirntln(2 + 0 + 1 + 8 + ”Welcome”);**

It evaluate the expression from left to right. First it perform arithmetic operation and add these 2 + 0 + 1 + 8 = 11, after this it perform concatenation,

**2 + 0 + 1 + 8 + “Welcome” => 2 + 1 + 8 + “Welcome” => 3 + 8 + “Welcome” => 11 + ”Welcome” => ”11Welcome”**

**System.out.println(“Welcome” + 2 + 0 + 1 + 8);**

Its also evaluates the expression from left to right. First it performs Concatenation between “Welcome” & 2, then the result is “Welcome2”, and again concatenation between “Welcome2” and 0, then result is “Welcome20” , and so on.

**“Welcome” + 2 + 0 + 1 + 8 => “Welcome2” + 0 + 1 + 8 => “Welcome20” + 1 + 8 => “Welcome201” + 8 => “Welcome2018”**