# Operators and Expressions

## Multiple Choice Questions (Tick the correct answers)

#### Question 1

The statement n += 4 is equivalent to:

1. ++n
2. n=n+4
3. n+1
4. none

#### Question 2

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

1. 10,10
2. 10,11
3. 11,10
4. 11,11

#### Question 3

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

1. 1
2. -1
3. 0
4. none

#### Question 4

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

1. 5.0
2. 5
3. 0
4. 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);?

1. 8
2. 64
3. 16
4. 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:

1. 55.0
2. 55.5
3. 55
4. none

#### Question 7

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

1. 11
2. 10
3. 8
4. 12

#### Question 8

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

1. 13
2. 14
3. 15
4. -15

## Write the Java expressions for the following

#### Question 1

ab + bc + ca

a * b + b * c + c * a

#### Question 2

a2 + ab - b2

a * a + a * b + b * b

#### Question 3

ut + (1/2)at2

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

uv / (u + v)

u * v / (u + v)

#### Question 5

(a + b)n

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

#### Question 6

2(lb+bh+lh)

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

a2 + b2

a * a + b * b

#### Question 8

x3 + xyz + y3

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;

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;

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);```

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;

30

##### Explanation

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

(b) 5* x++;

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);

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;

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

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

#### Question 1

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

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

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

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

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

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

(e) Unary operator

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

(f) Binary operator

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

#### Question 3

State the difference between = and ==

===
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

Arithmetical OperatorLogical 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)

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

`int a = 99;`
`int b = ++a;`
`int a = 99;`
`int b = a++;`