## Multiple Choice Questions

#### Question 1

Which of the following is the correct precedence of logical operators?

- !, &&, ||
- &&, !, ||
- ||, !, &&
- &&, ||, !

**Answer**

!, &&, ||

**Reason** — The correct precedence of logical operators is !, &&, ||.

#### Question 2

Choose the odd one out from the following:

- >
- ==
- &&
- <

**Answer**

&&

**Reason** — && is the logical AND whereas the other operators are relational.

#### Question 3

Given: String st = (a>= 90)? "Excellent": "Best";

Predict the output, when a = 90.

- Best
- Excellent: Best
- Best: Excellent
- Excellent

**Answer**

Excellent

**Reason** — Since condition (a>= 90) of ternary operator is true, st is assigned the value of expression 1 i.e., excellent.

#### Question 4

Choose the odd one out from the following:

- Unary operator
- Binary operator
- Ternary operator
- Bitwise operator

**Answer**

Bitwise operator

**Reason** — Unary operator, Binary operator and ternary operator are arithmetic operators.

#### Question 5

Given: x += x++ + ++x + --x

Find the value, when x=5.

- 23
- 21
- 22
- 24

**Answer**

23

**Reason** — The expression is calculated as follows:

```
x = x + (x++ + ++x + --x) (x = 5)
x = 5 + (5 + ++x + --x) (x = 6)
x = 5 + (5 + 7 + --x) (x = 7)
x = 5 + (5 + 7 + 6) (x = 6)
x = 5 + 18
x = 23
```

## State True or False

#### Question 1

The precedence of operators in Java follows BODMAS.**False**

#### Question 2

The output of a++ will be 1, if int a = -1.**False**

#### Question 3

The relational operators always result in terms of 'True' or 'False'.**True**

#### Question 4

Given: int m=5; m*=5 then the value stored in m results in 55.**False**

#### Question 5

The statement (a>b)&&(a>c) uses a logical operator.**True**

#### Question 6

If int a=27,b=4,c=0; then c = a % b; results in 3.**True**

#### Question 7

The statement p += 5 means p = p*5.**False**

#### Question 8

In the precedence of logical operators; NOT is followed by AND.**True**

## Write Java expressions

#### Question 1

$\sqrt[3]{\text{ab} + \text{cd}}$

**Answer**

`Math.cbrt(a * b + c * d)`

#### Question 2

$\text{p}^3 + \text{q}^4 - \dfrac{1}{2}\text{r}$

**Answer**

`Math.pow(p, 3) + Math.pow(q, 4) - (1.0 / 2.0 * r)`

#### Question 3

$\dfrac{-\text{b} + \sqrt{\text{b}^2 - 4\text{ac}}}{2\text{a}}$

**Answer**

`(-b + Math.sqrt((b * b) - (4 * a * c))) / (2.0 * a)`

#### Question 4

$\dfrac{0.05 -2\text{y}^3}{(\text{x} - \text{y})}$

**Answer**

`(0.05 - (2 * Math.pow(y,3))) / (x - y)`

#### Question 5

$\sqrt{\text{mn}} + \sqrt[3]{(\text{m} + \text{n})}$

**Answer**

`Math.sqrt(m * n) + Math.cbrt(m + n)`

#### Question 6

$\dfrac{3}{4}(\text{a} + \text{b}) - \dfrac{2}{5}\text{ab}$

**Answer**

`(3.0 / 4.0 * (a + b)) - (2.0 / 5.0 * a * b)`

#### Question 7

$\dfrac{3}{8}\sqrt{(\text{b}^2 + \text{c}^3)}$

**Answer**

`3.0 / 8.0 * Math.sqrt((Math.pow(b,2) + Math.pow(c,3)))`

#### Question 8

$\sqrt[3]{\text{a}} + \text{b}^2 - \sqrt[3]{\text{c}}$

**Answer**

`Math.cbrt(a) + Math.pow(b,2) - Math.cbrt(c)`

#### Question 9

$\dfrac{\sqrt{\text{a} + \text{b}^2 + \text{c}^3}}{3}$

**Answer**

`Math.sqrt(a + Math.pow(b,2) + Math.pow(c,3)) / 3`

#### Question 10

$\dfrac{\sqrt{3\text{x} + \text{x}^2}}{(\text{a} + \text{b})}$

**Answer**

`Math.sqrt(3 * x + Math.pow(x,2)) / (a + b)`

#### Question 11

$\text{z} = \text{x}^3 + \text{y}^3 - \dfrac{\text{y}}{\text{z}^3}$

**Answer**

`z = Math.pow(x,3) + Math.pow(y,3) - y / Math.pow(z,3)`

#### Question 12

$\text{q} = \dfrac{1}{\sqrt{\text{a} + \text{b}}} + \dfrac{3}{\text{c}^2}$

**Answer**

`q = 1 / Math.sqrt(a + b) + 3 / Math.pow(c,2)`

## Predict the output

#### Question 1

int c = (3<4)? 3*4 : 3+4;

##### Output

`12`

##### Explanation

As 3 is less than 4 so condition of ternary operator is true. Variable c is assigned the value of expression 1 which is 3 * 4 = 12.

#### Question 2

int a = 14, b = 4; boolean x = (a > b) ? true : false;

##### Output

`true`

##### Explanation

As 14 is greater than 4 so condition of ternary operator is true. Variable x is assigned the value of expression 1 which is true.

#### Question 3

int x = 90;

char c = (x<=90)? 'Z' : 'I';

##### Output

`Z`

##### Explanation

As value of x is 90 so condition of ternary operator is true. Variable c is assigned the value of expression 1 which is Z.

#### Question 4

int a = 18; int b = 12;

boolean t = (a > 20 && b < 15)? true : false;

##### Output

`false`

##### Explanation

The condition a > 20 is false as value of a is 18. So the logical AND operator — && returns false. Variable t is assigned the value of expression 2 which is false.

#### Question 5

c = (val + 550 < 1700)? 200: 400;

if: (a) val = 1000 (b) val = 1500

##### Output

```
(a) 200
(b) 400
```

##### Explanation

When val = 1000, val + 550 = 1550. As 1550 is less than 1700 so condition of ternary operator is true. Variable c is assigned the value of expression 1 which is 200.

When val = 1500, val + 550 = 2050. As 2050 is greater than 1700 so condition of ternary operator is false. Variable c is assigned the value of expression 2 which is 400.

## Answer the following questions

#### Question 1

What is an operator? What are the three main types of operators? Name them.

**Answer**

An operator is a symbol or sign used to specify an operation to be performed in Java programming.

The three main types of operators are Arithmetical, Logical and Relational.

#### Question 2

How is Java expression different from statement?

**Answer**

An expression is a set of variables, constants and operators i.e. an expression is a combination of operators and operands. When an expression is assigned to a variable, the complete set is referred to as a statement.

#### Question 3

Explain the following with one example each.

(a) Arithmetic 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) Ternary operator

**Answer**

Ternary operator operates on three operands. Its syntax is:

`condition ? expression 1 : expression 2`

Ternary operator evaluates the condition. If the condition is true then result of ternary operator is the value of expression 1. Otherwise the result is the value of expression 2.**Example:**`boolean isLeapYear = true;`

`int febDays = isLeapYear ? 29 : 28;`

Here, the ternary operator checks if the value of boolean variable `isLeapYear`

is true or false. As it is true, expression 1, which in this example is the value 29, is the result of the ternary operator. So, int variable `febDays`

becomes 29.

#### Question 4

Distinguish between:

(a) Unary & Binary arithmetic operator

**Answer**

Unary Arithmetic Operator | Binary Arithmetic Operator |
---|---|

It operates on a single operand | It operates on two operands |

Increment (++) and Decrement (--) operators are examples of Unary Arithmetic Operators | Multiplication (*) and Division (/) are examples of Binary Arithmetic Operators |

(b) Postfix increment and Prefix increment

**Answer**

Postfix Increment | Prefix Increment |
---|---|

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

The increment operator (++) is written after the operand. | The increment operator (++) is written before the operand. |

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. | Example:`int a = 99;` `int b = ++a;` After the execution of these two statements, both a and b will have the value of 100. |

(c) Postfix decrement and Prefix decrement

**Answer**

Postfix Decrement | Prefix Decrement |
---|---|

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

The decrement operator (--) is written after the operand. | The decrement operator (--) is written before the operand. |

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

(d) (p != q) and !(p == q)

**Answer**

(p != q) | !(p == q) |
---|---|

This expression uses the relational operator != (Not equal to) to determine if values of p and q are different. | This expression first checks if values of p and q are equal using the relational operator == (equality). It then inverts the result of equality operator using the logical NOT (!) operator to determine if values of p and q are different. |

#### Question 5

What is the difference between

(a) / and % operator?

**Answer**

/ | % |
---|---|

Division operator | Modulus operator |

Returns the quotient of division operation | Returns the remainder of division operation |

Example: int a = 5 / 2; Here a will get the value of 2 which is the quotient of this division operation | Example: int b = 5 % 2; Here b will get the value of 1 which is the remainder of this division operation |

(b) = 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. |

Example:`int a = 10;` This statement assigns 10 to variable `a` . | Example:`if (a == 10)` This statement checks if variable `a` is equal to 10 or not. |

#### Question 6(a)

What will be the output of the following code?

```
int k=5,j=9;
k += k++ - ++j + k;
System.out.println("k="+k);
System.out.println("j="+j);
```

##### Output

```
k=6
j=10
```

##### Explanation

k+= k++ - ++j + k

⇒ k = k + (k++ - ++j + k)

⇒ k = 5 + (5 - 10 + 6)

⇒ k = 5 + 1

⇒ k = 6

#### Question 6(b)

If int y =10 then find int z = (++y*(y++ + 5));

##### Output

```
z = 176
```

##### Explanation

z = (++y * (y++ + 5))

⇒ z = (11 * (11 + 5))

⇒ z = (11 * 16)

⇒ z = 176

#### Question 6(c)

Give the output of the following expression:

a += a++ + ++a + --a + a--; when a = 7;

##### Output

```
a = 39
```

##### Explanation

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

⇒ a = a + (a++ + ++a + --a + a--)

⇒ a = 7 + (7 + 9 + 8 + 8)

⇒ a = 7 + 32

⇒ a = 39

#### Question 6(d)

What is the value of y after the execution?

y += ++y + y-- + --y; when int y=8

##### Output

```
y = 33
```

##### Explanation

y+= ++y + y-- + --y

⇒ y = y + (++y + y-- + --y)

⇒ y = 8 + (9 + 9 + 7)

⇒ y = 8 + 25

⇒ y = 33

#### Question 7

Rewrite the following program segment using if-else statements instead of the ternary operator.

(a) String grade = (marks>=90)?"A": (marks>=80)? "B": "C";

**Answer**

```
String grade;
if (marks >= 90)
grade = "A";
else if (marks >= 80)
grade = "B";
else
grade = "C";
```

(b) commission = (sale > 5000) ? sale*10/100 : 0;

**Answer**

```
if (sale > 5000)
commission = sale * 10 / 100;
else
commission = 0;
```

(c) net = (salary > 10000) ? salary - (8.33/100)*salary : salary - (5/100)*salary

**Answer**

```
if (salary > 10000)
net = salary - (8.33/100) * salary;
else
net = salary - (5/100) * salary;
```

(d) s = (a + b < c || a + c <= b || b + c <= a) ? "Triangle is not possible": "Triangle is possible";

**Answer**

```
if (a + b < c || a + c <= b || b + c <= a)
s = "Triangle is not possible";
else
s = "Triangle is possible";
```

(e) c = (x >= 'A' && x <= 'Z') ? "Upper Case Letter" : "Lower Case Letter";

**Answer**

```
if (x >= 'A' && x <= 'Z')
c = "Upper Case Letter";
else
c = "Lower Case Letter";
```

#### Question 8

Rewrite the following using ternary operator.

(a)

```
if (x % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
```

**Answer**

`System.out.println(x % 2 == 0 ? "Even" : "Odd");`

(b)

```
if (bill > 10000)
discount=bill*10.0/100;
else
discount=bill*5.0/100;
```

**Answer**

`discount = bill > 10000 ? bill*10.0/100 : bill*5.0/100;`

(c)

```
if(income < 10000)
tax = 0;
else
tax = 12;
```

**Answer**

`tax = income < 10000 ? 0 : 12;`

(d)

```
if(a > b)
{
if (a > c)
g = a;
else
g = c;
}
else if (b > c)
g = b;
else
g = c;
```

**Answer**

`g = a > b ? a > c ? a : c : b > c ? b : c;`

(e)

```
if (p >= 4750)
k = p * 5 / 100;
else
k = p * 10 / 100;
```

**Answer**

`k = (p >= 4750) ? p * 5 / 100 : p * 10 / 100;`