Chapter 4
OPERATORS IN JAVA
I. Multiple Choice Questions:
Tick the correct answer:
1. The statement n+=4 is equivalent to
a) ++n b) n=n+4 c) n+1 d)none
2. What will be the output of a & b in the following, if int a,b;a=10;b=a++;
a) 10,10 b)10,11 c)11,10 d) 11,11
3. What will be the output of a++ when int a= -1;
a) 1 b) -1 c) 0 d) none
4. If int a=25, b=5,c=0; what value is stored in c? when c=a%b;
a)5. b) 5 c) 0 d) none
5. What is the result of the following in Java statement?
When int m=8; m*=8; [Link](“The output=”+m);
a)8 b) 64 c) 16 d) 88
6. 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
7. int m,p; m=5; p=0; p=m – – + – – m; The output will be
a) 11 b) 10 c) 8 d) 12
8. 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
Ans.
1. b) n=n+4
2. a) 10,10
3. b) -1
4. c) 0
5. b) 64
6. a) 55.0
7. c) 8
8. d) -15
II. Write the Java expressions for the following:
1. p=a2+bc Ans. p=a*a + b*c [or] [Link](a,2) +b*c
2. m= a2 – b2 Ans. m= (a*a – b*b) /(a*b)
ab
3. s=ut+ 1 at2 Ans. s=u*t + (1/2.0 ) *a*t*t
2
4. f= uv Ans. u*v/(u+v)
u+v
5. (a+b)2 +b Ans. [Link]((a+b),2)+b
6. y= 2(lb + bh + lh) Ans. 2* (l*b +b*h+l*h)
7. a2 + b2 Ans. a*a +b*b [or] [Link](a,2) + [Link](b,2)
8. z= x3 + y3 – xy Ans. z=[Link](x,3) + [Link](y,3) – (x*y)/3.0
3
III. Answer the following questions:
1. What is an operator?
Ans. An operator is basically a symbol or token, which performs arithmetic or logical
operations and gives meaningful result.
2. Name the different types of operators.
Ans. The three main types of operators in Java are Arithmetic operators, Relational operators
and Logical operators.
3. Explain the following:
a) Arithmetical operator b) Relational operator
c) Logical operator d) Unary operator
e) New operator f) Binary operator
Ans.
a) Arithmetical operator
The operators which are used to perform arithmetical calculations in a program are known
as arithmetical operators. Example +, -, *, / , % are the arithmetical operators used to
perform addition, subtraction, multiplication, division and modulus ( to get the remainder of
division) respectively.
b) Relational operator
These operators are used to show the relationship between the operands. Relational
operators like <,<=,>,>=,==,!= compare the values of the variables and result in true or false.
c) Logical Operator
Java uses logical operators (&&, ||, ! ) to yield true or false depending on the outcome of
different expressions.
AND operator (symbol: &&) results in true when all the conditions joined by AND are
true. Example : if(a>b && a>c &&a!=0)
OR operator (symbol: ||) results in true when atleast one of the conditions joined by
OR is true.
Example: if( a>0 || a<100)
NOT operator (symbol: !) is used when the reverse of the result of the expression. It
is a unary operator since it uses a single operand. Example: if(!a==10)
The above condition results in true when a is not equal to 10.
d) Unary operator
If an arithmetic operator is applied to a single operand it is known as a Unary operator (+, – ,
++, — etc)
Examples: a= – 36 ; b=++a;
e) new operator
The keyword new 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
An arithmetic operator applied to two operands is known as a Binary operator (+, -,
*. /, % etc)
Examples: int y=5%3; double p=5/2.0; double k=y+p;
4. What is a Ternary operator? Explain with the help of an example.
Ans. Ternary operators deal with three operands. It is also called conditional assignment
statement because the value assigned to a variable depends on a logical expression.
Syntax:
variable = (test expression)?Expression1 : Expression2;
The variable contains the expression1 if the test condition is true, otherwise it contains
expression2.(i.e. if the condition is false)
For example:
int x=20, y=25;
int small= (x<y)? x: y;
In the above example if the condition (x < y) is true, then the value of small=x otherwise the
value of small=y.
5. Differentiate between the following:
a) Arithmetical operator and Logical operator
b) Binary operator and Ternary operator
c) Logical AND(&&) and Logical OR(||)
d) Prefix operator and Postfix operator
e) [Link]() and [Link]()
Ans.
a) Arithmetical operator and Logical operator
In an expression when arithmetic operators are used, it usually results in some
numeric value whereas when logical operators are used, it results in either true or
false.
Arithmetical shorthand operators (+=, – =, *=, /=, %=) can be used to write short
hand expressions whereas logical operators cannot be used to write such shorthand
expressions.
b) Binary operator and Ternary operator
A binary operator deals with two operands (For example a +b) whereas a Ternary
operator deals with three operands (For example String x= “equal”, y= “unequal”;
String c= (a= =b) ?x : y;
The result of a binary operator does not always depend on the outcome of another
expression. whereas the outcome of a ternary operator always depends on the
expression given before the question mark.
c) Logical AND(&&) and Logical OR(||)
&& operator results in true when all the conditions joined by AND are true whereas
an || operator results in true when any one of the conditions joined by OR is true.
Example of &&:
if(a>b && a>c &&a!=0) .
The above expression results in true only when all three conditions result in true.
Example of ||:
if( a>0 || a<100).
The above expression results in true when any one of the above conditions result in true.
d) Prefix operator and Postfix operator
Although prefix and postfix operators are unary operators, prefix operators are
applied before the operand (for example ++a, – -b) whereas postfix operator are
applied after the operand ( for example a- -, m++)
Prefix operator works on the principle ‘change before action’ and the value of the
variable changes before the operation whereas Postfix operator works on the
principle ‘change after action’ and the value of the variable changes after the
operation.
e) [Link]() and [Link]()
[Link]( ) prints the statement and the cursor waits in the same line of the
screen whereas [Link]( ) prints the statement and the cursor jumps to
the next line and waits there.
[Link](“Hello”);
[Link](“ world”);
The above statements produces the output as follows:
Hello world
[Link](“Hello”);
[Link](“ world”);
The above statements produces the output as follows:
Hello
world
6. Differentiate between an operator and an expression.
Ans.
An operator is basically a symbol or token, which performs arithmetical or logical
operations to give meaningful result whereas an expression is a set of variables,
constants and arithmetical operators. In other words, an expression is a combination
of operators and operands.
Example:
% is an operator whereas y=x%2 is an expression.
7. if m=5 and n=2 then what will be the value of m and n after execution that will be stored
in (a) and (b)?
a) m – =n b) n = m+ m/n;
Ans.
Output
3
7
Working
a) m=m-n
m= 5 – 2
m=3
b) n= 5 + 5/2
n= 5 + 2
n=7
8. State the difference between = and = = .
Ans.
= is an assignment operator whereas = = is a relational operator.
Example: c=3 is used for assigning the value of 3 to c whereas if(c= =3) is used to
check whether the value of c is equal to 3.
9. What will be the output for the following program segment?
int a=0, b=10, c=40;
a= –b + c++ +b;
[Link](“a=”+a);
Ans.
Output
a=58
Working
a= 9 + 40 + 9
=58
10. What will be the output of the following?
if x=5
(a) 5 * ++x;
(b) 5 * x++;
Ans.
Output
30
25
Working
5*6=30
5*5=25
11. Evaluate the following expressions, if the values of the variables are:
a=2, b=3 and c=9
a) a – (b++) * (- -c);
b) a * (++b) %c
Ans.
Output
-22
8
Working
2 – 3*8= 2 – 24 = – 22
2 * (4)%9 = 8%9 = 8
[ Since * and % have the same precedence, left to right is followed]
12. If a=5 and b=9 calculate the value of:
a+= a++ – ++b +a;
Ans.
Output
6
Working
a= a +( a++ – ++b +a)
a= 5 + ( 5 – 10 + 6)
a= 5+ (1)
a= 6
13. Give the output of the program snippet.
int a=10, b=12;
if(a>=10)
a++;
else
++b;
[Link](“a=”+a+ “and b=”+b);
Ans.
Output
a=11and b=12
Working
if(10>=10) //true
10++;
else
++12;
[Link](“a=”+11+ “and b=”+12);
//b value 12 is printed because the control does’nt enter the else part.
14. Rewrite the following using ternary operator.
if(income<=100000)
tax=0;
else
tax=(0.1 * income);
Ans.
tax= (income <=100000)? 0: (0.1 * income);
15. Rewrite the following using ternary operator.
if( p>5000)
d=p*5/100;
else
d=2*p/10;
Ans.
d= (p>5000)? p*5/100 : 2*p/10;
Operators in Java
Class 9 - Logix Kips ICSE Computer Applications with BlueJ
Multiple Choice Questions
Question 1
An operator taking only single operand for its operation is called ...........
1. A unary operator ✓
2. A binary operator
3. A ternary operator
4. None of these
Question 2
Which one of the following is not a binary operator?
1. AND
2. %
3. ==
4. ! ✓
Explanation
! is the NOT operator that works on only one operand so it is a unary operator not a binary
operator.
Question 3
Which one of the following is not a valid operator in Java?
1. <=
2. !== ✓
3. !=
4. ==
Question 4
The statement i = i + 1 is equivalent to ...........
1. i++
2. i += 1
3. ++i
4. All of these ✓
Explanation
All the 3 statements increment the value of i by 1.
Question 5
For x = 5, the statement sum = ++x + 8 evaluates to ...........
1. sum = 13
2. sum = 14 ✓
3. sum = 15
4. sum = 16
Explanation
⇒ sum = 6 + 8
sum = ++x + 8
⇒ sum = 14
++x will first increment the value of x from 5 to 6 and then use this incremented value in the
expression.
Question 6
The statement (1>0) && (1<0) evaluates to ...........
1. 0
2. 1
3. false ✓
4. true
Explanation
1>0 is true and 1<0 is false so the condition becomes true && false. As && return true only when
both of its operands are true so the answer is false.
Question 7
The statement (1>0) || (1<0) evaluates to ...........
1. 0
2. 1
3. false
4. true ✓
Explanation
1>0 is true and 1<0 is false so the condition becomes true || false. As || return true if one or both of
its operands are true so the answer is true.
Question 8
The statement (1==1)? 1: 0 evaluates to ...........
1. 0
2. 1 ✓
3. false
4. true
Explanation
1==1 is true so the expression just after the question mark (which in this case is 1) is returned.
Question 9
The expression 13 % 3 gives the output ...........
1. 4
2. 3
3. 2
4. 1 ✓
Explanation
% is modulus operator that returns the remainder of the division operation. Dividing 13 by 3 gives
4 as quotient and 1 as remainder so the final answer is 1.
Question 10
The expression 13 / 3 gives the output ...........
1. 4 ✓
2. 3
3. 0
4. 1
Explanation
When both operands of division operator (/) are integers then integer division takes place discarding
the digits after the decimal sign.
Question 11
The statement [Link]("six " + 3 + 3); gives the output ...........
1. six 33 ✓
2. six 6
3. 33 six
4. 6 six
Explanation
First "six " + 3 is evaluated. As one operand of addition operator (+) is string and other int so int is
converted to string and concatenation is performed resulting in "six 3". Next, "six 3" + 3 is evaluated
and similarly the result is six 33.
Question 12
The expression 4 + 8 % 2 gives the output ...........
1. 6
2. 8
3. 4 ✓
4. None of these
Explanation
⇒4+0
4+8%2
⇒4
Due to operator precedence 8 % 2 is evaluated first. Its result is 0 so final result is 4.
State whether the given statements are True or False
Question 1
Arithmetic operators + and - also have a unary form.
True
Question 2
Operators = and == perform the same operation in Java.
False
Question 3
The expression 10 % 4 evaluates to 2.
True
Question 4
The expression 3 / 4 evaluates to 0.
True
Question 5
The expressions 3 + 4 and "3" + "4" evaluate to the same value.
False
Question 6
The expression x = x + 7 is same as x =+ 7.
False
Question 7
The new operator allocates memory during runtime.
True
Question 8
The statements x = 7 and x == 7 are same.
False
Question 9
The expression z =- 7 is same as z = z-7.
False
Question 10
The assignment operator (=) is a binary operator.
True
Assignment Questions
Question 1
What are logical operators? Give an example of each.
Answer
Logical operators operate only on boolean operands and are used to construct complex decision-
making expressions. Some of the logical operators are given below:
Operator Symbol
Logical AND &&
Logical OR ||
Logical NOT !
Question 2
What is an assignment operator? Give an example.
Answer
Assignment operator is used to assign the value of an expression to a variable. It has the following
syntax:
variable = expression;
For example,
totalMarks = 780;
The above statement assigns the value of 780 to the variable totalMarks. Any previous value stored
in totalMarks variable is overwritten by this new value.
Question 3
Explain the shorthand assignment operator with an example.
Answer
Java provides shorthand assignment operators for all the arithmetic binary operators. Shorthand
assignment operators follow the below syntax:
variable = variable operation expression;
Taking the example of shorthand addition operator, the expression x = x + 3 can be rewritten as x +=
3;
Question 4
What is the use and syntax of a ternary operator?
Answer
Ternary operator is used to check if a condition is true and false. Depending on whether the
condition tests true or false, expression 1 or expression 2 is evaluated. Its syntax is:
boolean-expression ? expression1 : expression2;
Question 5
State the difference between = and ==.
Answer
= ==
It is the assignment operator used for assigning a value It is the equality operator used to check if a variable is
to a variable. 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 6
If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a
Answer
⇒ a = a + (a++ - ++b + a)
a += a++ - ++b + a
⇒ a = 5 + (5 - 10 + 6)
⇒a=5+1
⇒a=6
a++ first uses the current value of a (which is 5) in the expression and then increments it to 6. ++b
first increment the current value of b to 10 and uses this incremented value in the expression.
Question 7
If x = 3, y = 7, calculate the value of:
x -= x++ - ++y
Answer
⇒ x = x - (x++ - ++y)
x -= x++ - ++y
⇒ x = 3 - (3 - 8)
⇒ x = 3 - (-5)
⇒x=3+5
⇒x=8
Question 8
What will be the output of the following if x=5?
i. 5 * ++x
ii. 5 * x++
Answer
⇒5*6
i. 5 * ++x
⇒ 30
⇒5*5
ii. 5 * x++
⇒ 25
Question 9
What is type conversion? How is an implicit type conversion different from explicit type conversion?
Answer
Type conversion is a process that converts a value of one data type to another data type.
In an implicit conversion, the result of a mixed mode expression is obtained in the higher most data
type of the variables without any intervention by the user. For example:
int a = 10;
float b = 25.5f, c;
c = a + b;
In case of explicit type conversion, the data gets converted to a type as specified by the programmer.
For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 10
What do you understand by type conversion?
Answer
Type conversion is a process that converts a value of one data type to another data type.
Question 11
What is typecasting in Java? Give an example.
Answer
The process of converting a value of one data type to another data type is called typecasting. For
example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 12
What are precedence and associativity?
Answer
Precedence of operators refers to the order in which the operators are applied to the operands in an
expression.
Associativity of operators refers to the direction of execution of operators ("Left to Right" or "Right to
Left") when operators in an expression have the same precedence.
Question 13
Evaluate the following expressions, if the values of the variables are a = 2, b = 3 and c = 3
i. a - (b++) * (--c)
ii. a * (++b) %c
Answer
⇒2-3*2
i. a - (b++) * (--c)
⇒2-6
⇒ -4
⇒2*4%3
ii. a * (++b) %c
⇒8%3
⇒2
Question 14
Write the Java expressions for the following:
i. (a + b)2 + b
Answer
[Link](a+b, 2) + b
ii. a2 + b2
Answer
a*a+b*b
iii. z = x3 + y3 + (xy) / 3
Answer
z = [Link](x, 3) + [Link](y, 3) + x * y / 3
iv. f = a2 + b2 / a2 - b2
Answer
f = (a * a + b * b) / (a * a - b * b)
v. z = ab + bc + ca / 3abc
Answer
z = (a * b + b * c + c * a) / (3 * a * b * c)
vi. 0 <= x <= 50
Answer
x >= 0 && x <= 50
Question 15
Rewrite the following statements without using shorthand operators.
a. p /= q
b. p -= 1
c. p *= q + r
d. p -= q - r
Answer
a. p = p / q
b. p = p - 1
c. p = p * (q + r)
d. p = p - (q - r)
Question 16
Determine the output of the following program.
public class Test
public static void main(String[] args)
{
int a = 1, b = 2;
[Link]("Output1: " + a + b);
[Link]("Output2: " + (a + b));
Output
Output1: 12
Output2: 3
Explanation
In the first println statement, the expression is "Output1: " + a + b. First "Output1: " + a is evaluated.
As one operand of addition operator is string and other is int so int is casted to string and
concatenated giving the result as "Output1: 1". After that, "Output1: 1" + 2 is evaluated and similarly
the result is Output1: 12.
In second println statement, the expression is "Output2: " + (a + b). Due to brackets, (a + b) is
evaluated first. As both operands are integers so they are added giving the result as 3. After that,
"Output2: " + 3 is evaluated, resulting in "Output2: 3".
Question 17
What is the difference between the following two statements? Explain the results.
x -= 5;
x =- 5;
Answer
The first statement, x -= 5; subtracts 5 from x and assigns the result back to x. It is equivalent to x = x
- 5;
The second statement, x =- 5; assigns the value of -5 to x.
Question 18
What is concatenation? On which data type is concatenation performed?
Answer
Concatenation means joining two strings together. It is performed on String data type.
Question 19
Determine the output of the following program.
public class PredictOutput1
public static void main(String args[])
{
int a = 4, b = 2, c = 3;
[Link]("Output 1: " + (a = b * c));
[Link]("Output 2: " + (a = (b * c)));
Output
Output 1: 6
Output 2: 6
Explanation
In the first println statement, the expression is (a = b * c). * has higher precedence than = so first b
and c are multiplied and after that the result is assigned to a. Assignment operator returns the value
of the assignment so the result of this (a = b * c) entire expression is 6.
In the second println statement, the expression is (a = (b * c)). Putting b * c in brackets causes b * c to
be evaluated first but the result is same as the first println statement as * has higher precedence
than =.
Question 20
Determine the output of the following program.
public class PredictOutput2
public static void main(String args[])
int a = 6, b = 2, c = 3;
[Link]("Output 1: " + (a == b * c));
[Link]("Output 2: " + (a == (b * c)));
Output
Output 1: true
Output 2: true
Explanation
b * c results in 6 which is equal to the value of a so true is printed in both the cases.
Question 21
Determine the output of the following program.
public class PredictOutput3
public static void main(String args[])
int a = 2, b = 2, c = 2;
[Link]("Output 1: " + (a + 2 < b * c));
[Link]("Output 2: " + (a + 2 < (b * c)));
Output
Output 1: false
Output 2: false
Explanation
In the first println statement, the expression is (a + 2 < b * c). b * c is evaluated first as * has higher
precedence than + and <. After that a + 2 is evaluated as between + and <, + has higher precedence.
Comparison is done in the end. As 4 < 4 is false so false is printed. The case of second println
statement is similar.