Operators and
Expressions in Java
[ Highlights
l. An ·operator is a token use<! to perform operations and
yield
in a result.
2. ~per ands are the contents on which any operation is
carried out.
3. Unary operator is applied with asingle operand. ~.
4. Binary operators use two operands.
..
-
.
5. •Ternary operator works with three operands which i:5
also called conditional
l _ assignment operartor. • •
6. A logical operator is a token which results in either true
or false.
·7_ Relations operators ar; us~ to~~tab~~ rel~tion -~a;th~ opera
nds.
8. Bitwise operators are used to process operands on bit
level by using binary digits.
Very Short Answer Type Questions (1 or 2 marks)
1. What is an operator?
Ans. An operator is a token which performs an operation and
yield in a result.
2. What do you mean by an operand?
Ans. Operands are the contents on which an operation is
perfo ~ed to obtain a
meaningful result.
3• Differentiate between an arithmetic expression and an arithmetic stat
. .
Ans. An arithmetic expression . ement.
1s formed by using constants, variables and arith m .
· ·
operators. Ass1grung an·thme ti.c expression•
to a variable is know netic as
arithmetic statement.
e.g.
Arthmetic expression - b * b - 4 * a * c
¥ Arithmetic statement - d = b * b - 4 * a * c
1\11-;4. What do you mean by un~ry _operat~r?
~ - An arithmetic operator which IS applied to a single
operand is known as
/-- ~~~
~~
e • g • .• +I _I ++, - -, etc.
s. What is the purpose _of unary (-) operator?
Ans. A unary (-) operator is use_d to revert the sign of
an operand
e.g.: if a = -8 , then - a will return 8.
•
6. Differentiate between in_crement and decrement opera
tors.
An increment operator increases the value of an
Ans.
decrement operator decreases the value by 1. operand b
Y 1, Whereas a
20
7. Different iate between prefix and p ostfix operators.
. ion. Postfix
fix ope rat or (- - or ++) upd ates the operand before the act
Ans. A pre operands after the action.
operator ( - - or ++) updates the
ors?
en logical and relational operat
8. What is the similarity betwe
I Ans. Both the operators result in
i.e.: true or false.
Boolean type value.
9. What is bitwise operator? rands.
which works on bit level of ope
Ans. A bitwise operator is one
or 1):
i.e.: based on binary values ( O
left and shift right operators?
10. Differentiate between shi ft d towards left by
s. Shi ft left (<<) ope rat or shi fts the bit pattern of the operan of the
An
ber of bits . Shi ft rig ht (>>) operator shifts the bit pattern
defined num •
d number of bits.
operand towards right by define
or 4 Marks)
Short Answer Type Questions (3 er-related?
operands are int
1. In what ways operators and ngful
use s ope ran ds to per for m any operation in order to give meani
Ans. An operator er.
In this wa y, ope rat ors and ope rands are inter-related to each oth
result.
expression?
2. What do you mean by pure e data type.
pur e exp res sio n is one wh ich contains the operands having sam
Ans. A
e.g.: int a,b;
a + b ; I I pure expression
3. What is a mixed expression? types.
s the operands of different data
Ans. A mixed expression contain
e.g.: int a; float b,c;
b + a*c; I I mixed expression
implemented?
4. What is coercion? Ho w is it xed
con ver sio n of an exp res sion is termed as coercion. In a mi
Ans. Implicit type e which
sio n, the dat a typ e get s aut om atically converted into higher typ
expres sion. .
is known as implicit conversio
n. This is a default type conver
[ICSE 2007]
at do you me an by typ e cas ting?
5. Wh a mixed expression,
s. Exp licit typ e con ver sio n is called type casting. Sometimes, in r's
An
be nee ded to get con ver ted int o another type based on the use
the data may , then it is
. If the con ver sio n of dat a type is based on use r's choice
demand
Type Casting.
known as Explicit conversion or
Syntax: (type) Expression
e.g.: int a; float b;
(int) a+b;
ed as int type.
Here, the sum of (a+b) is obtain
le.
6. What do you mean by Ter
nary operator? Give an examp
nt operator. In this
s. A ter nar y ope rat or is als o referred as conditional assignme
An
ues are based on a condition.
system, the assignment of the val
e.g.: c = (a>b)? a:b; en condition
giv en exa mp le, the val ue of a is assigned to c only if the giv
In the
is true, b otherwise.
21
7• Write the equivalent Java Expressions for the following matbematical
expressions:
(a) (a+bc)/d (b) (a3+b3)/3ab (c) (0.5 - y3)/(x+y)
4
(d) (.fa" +b )/ab (e) 1.5 - 1.0e·5
(f) Wx' + ..Jy)/Math.sqrt(x+y)
Ans. (a) (a+b*c)/ d
(b) ((Math.pow(a,3)+Math.pow(b,3))/ (3*a*b)
(c) (O.S - Math.pow(y,3))/(x+y)
(d) (Math.sqrt(a)+Math.pow(b,4))/a*b
(e) 1.5 - Math.exp(-5)
(f) (Math.sqrt(x)+Math.sqrt(y))/Math.sqrt(x+y)
8. Give the data type of the following expressions. Also indicate the type of
conversion taking place in it.
(a) int a; char c; byte b;
a+c/b;
(b) char c; float f; double d;
c*f/d;
(c) int i,j; float f; double d;
(int)(i+f)/(j*d);
(d) int x; char y; float z;
z*(x+y);
(e) double d; float a; char b;
(int)d*b+a;
Ans. (a) int : Implicit (b) double : Implicit (c) int : Explicit
(d) float : Implicit (e) int : Explicit
9. In the given expression, show step by step conversion of data type:
int i; char c; float f; double d;
i + c*f/d
Ans. f
1 + C * I d
char float
t
l
int
I
i
I double
float
double
double
10_ Write short cut java expressions for each of the followin .
(a) p = p + d (b) c = c - 20 g.
(d) d = d/10 (e) x = x % 2 (c) t = t * 0.05
Ans. (a) p += d (b) c - = 20
(d) d / =10 (e) x % =2 (c) t * == 0.05
11_ What is ariti:ime~c hierar~hy? .
An arithmetic hierarchy 1s the rule of using 'thm .
Ans.
( is simply termed as (BEDMAS - Bracket E an etic operators Th
, xponent, Division, Mu} . .e rule
tiplicationI
22
Additio n and Subtraction).
Multiplic~tion (*) and divisio n (/) have the same precedence but one which
it is
appear s first from the left side of an expression is operate d first. Similarly,
true for additio n and subtraction.
12. Give the results of the following expressions:
(a) ++a + a++ + 4; if a = 2 (b) a· - + ++a%4; if a =5
(c) 3*a++ + ++a*4; if a = 2 (d) (a++ * ++a)/5; if a =5
(e) (a++ + ++a)/(a+3); if a = 3
Ans. (a) 10 (b) 6 (c) 22 (d) 7 (e) 1
13. Give the output of the follow ing expressions:
total = (val + 500 > 1500)? 200 : 400;
if (a) val = 1000 (b) val = 1500; initiall y
Ans. (a) total =400 (b) total = 200
14. What is the result of the follow ing statem ent, when executed?
boolean val = (amt > 5000_)? true:false;
(a) amt =3000 • (b) amt = 8000 initially.
Ans. (a) false (b) True
15. What is the outcome when follow ing statem ents are execut ed?
(a) String s=(a==b&& a==c)? "Equil ateral" : "Isosce les";
(i) a=5, b=8, c=5 (ii) a=6, b-6, c=6
(b) int Tax=(amt>100000)? 5.0/100.0*amt : 0; If
(i) amt = 50000 (ii) amt = 150000
Ans. (a) (1) Isosceles (ii) Equilateral
(b) (1) 0 (ii) 7500
16. Give the output of the following statem ents when execut ed:
(a) System.out.println("The sum = ";a+b); if a = 4 and b = 2
(b) System.out.println("The sum = ";(a+b)); if a = 4 and b = 2
(c) System.out.println("The sum = ";a++ +4); if a = 2
(d) System.out.println("The sum = ";(a++ +4)); if a = 2
(e) System.out.println((i++ <10)); if i =10
(f) System.out.println((++ i >10)); if i =10
Ans. (a) The sum = 42
(b) The sum =6 .
(c) The sum= 24
(d) The sum = 6
(e) false
(f) true
17. Give the output of the following statements when execut ed·
If a =12 and b = 10 •
(a) System.out.println((a&b)); (b) System .out.pr intln(( a Ib));
(c) System.out.println((aAb)); (d) System.out.println((a<<2));
(e) System .out.pri ntln((a> >2)); (f) System.out.println((14>>>2));
Ans. (a) 8 (b) 14 (c) 6 (d) 48 (e) 3 (f) 3
23