## Multiple Choice Questions

### Tick the correct answer:

**The statement**`n += 4;`

is equivalent to:

a.`++n;`

b.`n = n + 4;`

c.`n + 1;`

d. None**What will be the output of**`a`

and`b`

in the following:

`int a, b;`

a = 10;

b = a++;

a. 10, 10

b. 10, 11

c.**11, 10**

d. 11, 11**What will be the output of**`a++;`

when`int a = -1;`

a. 1

b. -1

c.**0**

d. None**If**`int a = 25, b = 5, c = 0;`

What value is stored in`c`

, when`c = a % b;`

?

a. 5.0

b. 5

c.**0**

d. None**What is the result of the following in Java statement, when**

`int m = 8;`

m *= 8;

System.out.println("The output = " + m);

a. 8

b.**64**

c. 16

d. 88`double c;`

int x, y, z;

x = 5;

y = 10;

z = 11;

c = x * y + z / 2;

**The value stored in**`c`

is:

a.**55.0**

b. 55.5

c. 55

d. None`int m, p;`

m = 5;

p = 0;

p = m-- + --m;

**The output will be:**

a. 11

b. 10

c.**8**

d. 12`int a = 7, p = 0, q = 0;`

p = ++a + --a;

q -= p;

**The output of**`q`

will be:

a. 13

b. 14

c. 15

d.**-15**

### Write the Java expressions for the following:

**p = a**^{2}+ bc

`p = a * a + b * c;`

**m = (a**^{2}– b^{2}) ÷ ab

`m = (a * a - b * b) / (a * b);`

**s = ut + ½ at**^{2 s = u * t + 1.0 / 2 * a * t * t;}**f = (uv) ÷ (u + v)**

`f = (u * v) / (u + v);`

**(a + b)**^{2}+ b

`(a + b) * (a + b) + b;`

**y = 2(lb + bh + lh)**

`y = 2 * (l * b + b * h + l * h);`

**a**^{2}+ b^{2 }`a * a + b * b;`

**z = x**^{3}+ y^{3}– xy ÷ 3

`z = x * x * x + y * y + y - x * y / 3;`

### Answer the following questions:

**What is an operator?**

An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result.**Name the different types of operators.**

The different types of operators are:

a. Unary

b. Binary

c. Ternary.**Explain the following:**

a.**Arithmetical operator:**The operators used to perform arithmetical calculations in a program are known as arithmetical operators.

b.**Relational operator:**The operators used to show the relationship between the operands by comparing the values of the variables and give the result as true or false, are known as relational operators.

c.**Logical operator:**AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.

d.**Unary operator:**The operators applied to a single operand is known as unary operator.

e.**new operator:**The new operator is used to allocate space in the dynamic memory for the storage of data and functions belonging to an object in Java programming.

f.**Binary operator:**The operators that deal with two operands is known as binary operator.**What is a ternary operator? Explain with the help of an example.**

The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.

Example:

int a = 5:

int b = 3;

int max = (a > b)? a : b;**Differentiate between the following:**

a.**Arithmetical operator and logical operator:**The operators used to perform arithmetical calculations in a program are known as arithmetical operators. AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.

b.**Binary operator and Ternary operator:**The operators that deal with two operands is known as binary operator. The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.

c.**Logical AND and Logical OR:**Logical AND combines two or more boolean expressions in such a way that it results in true if all the expressions are true, otherwise it returns false. Logical OR combines two or more boolean expressions in such a way that it will return true when any one condition is true, otherwise it returns false.

d.**Prefix operator and Postfix operator:**Prefix means that the value of the variable changes before the operation takes place. Postfix means that the operand will change after performing the operation.

e.**System.out.print() and System.out.println():**When we use print(), the cursor remains in the same line after producing the result. But in println(), the cursor shifts to the next line after producing the result.**Differentiate between an operator and an expression.**

An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result. An expression is any meaningful combination of variables, constants and/or operators, that, when calculated, yields a value.**If m = 5 and n = 2, then what will be the output of m and n after execution that will store in (a) and (b)?**

a.`m -= n;`

m = m – n;

= 5 – 2

= 3

b.`n = m + m / n;`

n = 5 + 5 / 2

= 5 + 2

= 7**State the difference between = and ==.**

= is an assignment operator. It assigns a value to a variable. == is a relational operator. It compares two values for equality.**What will be the output of the following program segment?**

**int a = 0, b = 10, c = 40;**

**a = --b + c++ + b;**

**System.out.println("a = " + a);**

a = 9 + 40 + 9

= 58.**What will be the output of the following, if x = 5;?**

a.`5 * ++x`

= 5 * 6

= 30.

b.`5 * x++`

= 5 * 5

= 25.**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 – 24

= -22.

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

= 2 * 4 % 9

= 2 * 4

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

`a += a++ - ++b + a;`

`a = a + a++ - ++b + a;`

= 5 + 5 – 10 + 6

= 6.**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);**

a = 11 and b = 12**Rewrite the following using ternary operator:**

**if(income <= 100000)**

**tax = 0;**

**else**

**tax = (0.1 * income);**

`tax = (income <= 100000)? 0 : (0.1 * income);`

**Rewrite the following using ternary operator:**

**if(p > 5000)**

**d = p * 5 / 100;**

**else**

**d = 2 * p / 10;**

`d = (p > 5000)? p * 5 / 100 : 2 * p / 10;`

## 18 replies on “Operators in Java”

Was very helpful

I have made further changes to the program. Please check.

Sir Can u teach Jave in a simple and brief way

Sure. I have planned to write a book on Programming in Java for Beginners.

It is very helpful for me

Very helpful for me.

Sir, In multiple choice Question.in question 2 .

why the answer will be 11, 10

because later ‘a’ updates itself to 11. But ‘b’ gets older value of a, due to post-increment.

Sir,

Is it possible to break out of nested loops in Java?

Yes. But nested loops are not included in the mid-term exams.

Excellent job sir although I am a student of sec B I am within this circle.

Java case sensitive language

Yes, Java is a case-sensitive language.

Sir , in unit test syllabus class 9 … 1st 5 chaps and programming from class 8 booklet r coming

Yes.

Helpfull

Thanks Soudip!

Sir where is the remaining questions means question IV which is named as unsolved java programming in the book.please solve those as well.

OK. I will post it soon.