OPERATORS IN JAVA Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

OPERATORS IN JAVA

Multiple Choice Questions

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 & 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++ when 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

1|Page
3. 0 ✓

4. none

Question 5

What is the result of the following in Java statement?

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

Explanation

c=x*y+z/2
⇒ c = 5 * 10 + 11 / 2
⇒ c = 5 * 10 + 5
⇒ c = 50 + 5
⇒ c = 55.0

2|Page
Question 7

int m, p;
m = 5;
p = 0;
p = m-- + --m;

The output will be:

1. 11
2. 10

3. 8 ✓

4. 12

Explanation

p = m-- + --m;
⇒p=5+3
⇒p=8

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 ✓

Explanation

p = ++a + --a
⇒p=8+7
⇒ p = 15

3|Page
q -= p
⇒q=q-p
⇒ q = 0 - 15
⇒ q = -15

Write the Java expressions for the following:

Question 1

p = a2 + bc

Answer

p=a*a+b*c

Question 2

m = a2 - b2 / (ab)

Answer

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

Question 3

s = ut + (1/2)at2

Answer

s = u * t + (1.0 / 2) * a * t * t

Question 4

f = uv / (u + v)

Answer

f = u * v / (u + v)

Question 5

4|Page
(a + b)2 + b

Answer

(a + b) * (a + b) + b

Question 6

y = 2(lb + bh + lh)

Answer

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

Question 7

a2 + b 2

Answer

a*a+b*b

Question 8

z = x3 + y3 - xy / 3

Answer

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

Answer the following questions

Question 1

What is an operator?

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


programming.

Question 2

5|Page
Name the different types of operators.

The different types of operators are Arithmetical, Logical and Relational.

Question 3

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

6|Page
these true and false boolean values and gives a resultant boolean value as false. So, boolean
variable c becomes false.
(d) Unary operator
Operators that act on one operand are called as Unary operators. Unary +, unary -, ++, --, etc. are
some unary operators in Java.

(e) new operator


new operator is used to instantiate an object by dynamically allocating memory for it.

(f) Binary operator


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

Question 4

What is a Ternary operator? Explain with the help of an example.

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 5

Differentiate between the following:

(a) Arithmetical operator and Logical operator

Arithmetical
Logical Operator
Operator

Arithmetic operators
Logical operators operate on boolean expressions to
are used to perform
combine the results of these boolean expression into
mathematical

7|Page
Arithmetical
Logical Operator
Operator

operations. a single boolean value.

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

(b) Binary operator and Ternary operator

Binary operator Ternary operator

Binary operators work on


Ternary operator work on three operands.
two operands.

+, -, *, /, etc. are a few


The conditional operator ? : is a Ternary
examples of Binary
operator.
operators.

(c) Logical AND (&&) and Logical OR(||)

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

It evaluates to true only if both of It evaluates to true if one or both of its


its operands are true. operands are true.

Example: Example:
int a = 8, b = 13, c = 0; int a = 8, b = 13, c = 0;
if (a > 10 && b > 10) if (a > 10 || b > 10)
c = 10; c = 10;
else else

8|Page
Logical AND (&&) Logical OR(||)

c = 5; c = 5;
Here, value of c will be 5 as one of Here, value of c will be 10 as at least
the operands is false. one of the operands is true.

(d) Prefix operator and Postfix operator

Prefix Operator Postfix Operator

It works on the principle of It works on the principle of USE-


CHANGE-THEN-USE. THEN-CHANGE.

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

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

(e) System.out.print( ) and System.out.println( )

System.out.print( ) System.out.println( )

It prints data to the console but the cursor


It prints data to the console and
remains at the end of the data in the same
places the cursor in the next line.
line.

Next printing takes place from the same Next printing takes place from next
line. line.

Question 6

9|Page
Differentiate between an operator and an expression.

An operator is a symbol or sign used to specify an operation to be performed whereas an


expression is a set of variables, constants and operators i.e. an expression is a combination of
operators and operands.

Question 7

If m=5 and n=2 then what will be the output of m and n after execution that will store in (a) & (b)?

(a) m -= n;

Answer

m -= n
⇒m=m-n
⇒m=5-2
⇒m=3

(b) n = m + m/n;

Answer

n = m + m/n
⇒n=5+5/2
⇒n=5+2
⇒n=7

Question 8

State the difference between = and ==.

= ==

It is the assignment
operator used for It is the equality operator used to check if a variable
assigning a value to is equal to another variable or literal.
a variable.

10 | P a g e
= ==

E.g. int a = 10;


E.g. if (a == 10) checks if variable a is equal to 10 or
assigns 10 to
not.
variable a.

Question 9

What will be the output for the following 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 10

What will be the output of the following?


if x =5

(a) 5* ++x;

Answer

5 * ++x
⇒5*6
⇒ 30

(b) 5* x++;

11 | P a g e
Answer

5 * x++
⇒5*5
⇒ 25

Question 11

Evaluate the following expressions, if the values of the variables are:


a = 2, b = 3, and c = 9

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

Answer

a - (b++) * (--c)
⇒2-3*8
⇒2-3*8
⇒ 2 - 24
⇒ -22

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

Answer

a * (++b) % c
⇒ a * (++b) % c
⇒ 2 * (4) % 9
⇒8%9
⇒8

Question 12

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


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

Answer

a += a++ - ++b + a
⇒ a = a + (a++ - ++b + a)
⇒ a = 5 + (5 - 10 + 6)

12 | P a g e
⇒a=5+1
⇒a=6

Question 13

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

Output

a = 11 and b = 12

Explanation

The condition if(a>=10) is true so a++ increments a to 11. b remains the same.

Question 14

Rewrite the following using ternary operator.

if(income<=100000)
tax = 0;
else
tax = (0.1*income);

Answer

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

Question 15

Rewrite the following using ternary operator.

13 | P a g e
if(p>5000)
d = p*5/100;
else
d = 2*p/100;

Answer

d = p > 5000 ? p * 5 / 100 : 2 * p / 100;

14 | P a g e

You might also like