# Chapter 4 Unit 2: Operators and Expressions

**Contents**

- Multiple Choice Questions (Tick the correct answers)
- Write the Java expressions for the following
- Predict the output for the following
- Answer the following questions

## Multiple Choice Questions (Tick the correct answers)

#### Question 1

The statement n += 4 is equivalent to:

- ++n
**n=n+4**- n+1
- none

#### Question 2

What will be the output of 'a' and 'b' in the following if int a, b; a=10; b=a++;?

- 10,10
- 10,11
**11,10**- 11,11

#### Question 3

What will be the output of a++ if int a = -1;?

- 1
- -1
**0**- none

#### Question 4

If int a=25, b=5,c=0; what value is stored in c when c = a%b;?

- 5.0
- 5
**0**- none

#### Question 5

What is the result of the following statement in Java: When int m=8; m*=8; System.out.println("The output ="+m);?

- 8
**64**- 16
- 88

#### Question 6

double c;int x,y,z; x=5;y=10;z=11; c=x*y+z/2; The value stored in c is:

**55.0**- 55.5
- 55
- none

#### Question 7

int m,p;m=5;p=0; p= m-- + --m; The output will be:

- 11
- 10
**8**- 12

#### Question 8

int a=7, p=0, q=0; p = ++a + --a; q -= p; The output of q will be:

- 13
- 14
- 15
**-15**

## Write the Java expressions for the following

#### Question 1

ab + bc + ca

#### Answer

**a * b + b * c + c * a**

#### Question 2

a^{2} + ab - b^{2}

#### Answer

**a * a + a * b + b * b**

#### Question 3

ut + (1/2)at^{2}

#### Answer

**u * t + (1 / 2) * a * t * t**

#### Question 4

uv / (u + v)

#### Answer

**u * v / (u + v)**

#### Question 5

(a + b)^{n}

#### Answer

**Math.pow((a + b), n)**

#### Question 6

2(lb+bh+lh)

#### Answer

**2 * (l * b + b * h + l * h)**

#### Question 7

a^{2} + b^{2}

#### Answer

**a * a + b * b**

#### Question 8

x^{3} + xyz + y^{3}

#### Answer

**x * x * x + x * y * z + y * y * y**

## Predict the output for the following

#### Question 1

If m = 5 and n = 2, predict the output values of m and n:

(a) m -= n;

##### Output

**m = 3****n = 2**

##### Explanation

**m -= n**

⇒ **m = m - n**

⇒ **m = 5 - 2**

⇒ **m = 3****Value of n is unchanged so it is 2.**

(b) n = m + m/n;

##### Output

**m = 5****n = 7**

##### Explanation

**n = m + m/n;**

⇒ **n = 5 + 5/2;**

⇒ **n = 5 + 2;** **[∵ 5/2 is integer division so result is 2]**

⇒ **n = 7****Value of m is unchanged so it is 5.**

#### Question 2

What will be the output from the 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**

#### Question 3

What will be the output of the following if x = 5 initially?

(a) 5* ++x;

##### Output

**30**

##### Explanation

**5* ++x****⇒ 5* 6 [∵ ++x will first increment x to 6 and then use it in the expression]****⇒ 30**

(b) 5* x++;

##### Output

**25**

##### Explanation

**5* x++****⇒ 5* 5 [∵ x++ will first use the current value of x in the expression which is 5. After that x is incremented to 6]****⇒ 25**

#### Question 4

Evaluate the following expressions if the values of the variables are a = 2, b = 3, and c = 9.

(a) a - (b++) * (--c);

##### Output

**22**

##### Explanation

**a - (b++) * (--c)****⇒ 2 - 3 * 8 [∵ b++ uses current value of b in expression and then increments it, --c decrements c to 8 and then uses this 8 in the expression]****⇒ 2 - 24****⇒ -22**

(b) a * (++b) % c;

##### Output

**8**

##### Explanation

**a * (++b) % c**

⇒ **2 * 4 % 9** **[∵ ++b increments b to 4 then uses it in the expression]**

⇒ **8 % 9**

⇒ **8**

#### Question 5

If a = 5, b = 9, calculate the value of:

a += a++ - ++b + a

##### Output

**6**

##### Explanation

**a += a++ - ++b + a**

⇒ **a = a + (a++ - ++b + a)**

⇒ **a = 5 + (5 - 10 + 6) [∵ a++ will first use current value of a then increment it to 6. ++b will increment b to 10 and use the incremented value. As a++ incremented a to 6 so the value of last a in the expression is 6]**

⇒ **a = 5 + 1**

⇒ **a = 6**

## Answer the following questions

#### Question 1

What is an operator? Name the different types of operators.

#### Answer

**An operator is a symbol or sign used to specify an operation to be performed in Java programming. The different types of operators are Arithmetical, Logical and Relational.**

#### Question 2

Explain the following:

(a) Arithmetical operator

#### Answer

**Arithmetic operators are used to perform mathematical operations on its operands. Operands of arithmetic operators must be of numeric type. A few arithmetic operators operate upon one operand. They are called Unary Arithmetic operators. Other arithmetic operators operate upon two operands. They are called Binary Arithmetic operators. As an example consider the below statement:**`int a = 10 + 20;`

**Here, the addition arithmetic operator, represented by the symbol + will add 10 and 20. So variable a will be 30.**

(b) Relational operator

#### Answer

**Relational operators are used to determine the relationship between the operands. Relational operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The result of an operation involving relation operators is a boolean value — true or false.****Example:**`int a = 8;`

`int b = 10;`

`boolean c = a < b;`

**Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.**

(c) Logical operator

#### Answer

**Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value.****Example:**`int a = 7;`

`int b = 10;`

`boolean c = a < b && a % 2 == 0;`

**Here, the result of first boolean expression a < b is true and the result of second boolean expression a % 2 is false. The logical AND operator ( && ) combines these true and false boolean values and gives a resultant boolean value as false. So, boolean variable c becomes false.**

(d) Arithmetic expression

#### Answer

**An Arithmetic expression contains variables, constants and arithmetical operators together to produce meaningful results.**

(e) Unary operator

#### Answer

**Operators that act on one operand are called as Unary operators. Unary +, unary -, ++, --, etc. are some unary operators in Java.**

(f) Binary operator

#### Answer

**Operators that act on two operands are called as Binary operators.**

#### Question 3

State the difference between = and ==

#### Answer

= | == |
---|---|

It is the assignment operator used for assigning a value to a variable. | It is the equality operator used to check if a 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. |

#### Question 4

Differentiate between the following:

(a) Arithmetical and Logical operator

#### Answer

Arithmetical Operator | Logical Operator |
---|---|

Arithmetic operators are used to perform mathematical operations. | Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value. |

+, -, *, /, etc. are a few examples of Arithmetic operators. | &&, ||, ! are a few examples of Logical Operators |

(b) Logical AND (&&) and Logical OR (II)

#### Answer

Logical AND (&&) | Logical OR(||) |
---|---|

It evaluates to true only if both of its operands are true. | It evaluates to true if one or both of its operands are true. |

Example: int a = 8, b = 13, c = 0; if (a > 10 && b > 10) c = 10; else c = 5; Here, value of c will be 5 as one of the operands is false. | Example: int a = 8, b = 13, c = 0; if (a > 10 || b > 10) c = 10; else c = 5; Here, value of c will be 10 as at least one of the operands is true. |

(c) Prefix and Postfix

#### Answer

Prefix Operator | Postfix Operator |
---|---|

It works on the principle of CHANGE-THEN-USE. | It works on the principle of USE-THEN-CHANGE. |

It is written before the operand. | It is written after the operand. |

Example:`int a = 99;` `int b = ++a;` After the execution of these two statements, both a and b will have the value of 100. | Example:`int a = 99;` `int b = a++;` After the execution of these two statements, a will have the value of 100 and b will have the value of 99. |