Chapter 6: Operators and Expressions
Fill in the blanks
The statement n++ is equivalent to n = n + 1.
If a=10;b=++a, then value of a and b will be 11 and 11.
If a= -1 , then the output of a++ will be 0.
If int a=43;int b=5;int c=0; and c = a%b; then the value of c will be 3.
If int m=8; m*=8; then the output of m will be 64.
If x=5;y=10;z=11; and c = x++ * 5 + --y * ++z; then the value of c will be 133.
If m=5;p=0;and p = ++m + --m; the output will be 11.
If int a=7;int p=0; and p = ++a + - -a; the value of p will be 16.
The binary equivalent of (45)10 is 101101.
If a=0,b=1; then the output of bitwise OR will be 1.
Write the Java statements for the following
p = a2 + bc
p = a * a + b * c
m = a2 - b2 / (ab)
m = (a * a - b * b) / (a * b)
s = ut + (1/2)at2
s = u * t + (1 / 2) * a * t * t
f = uv / (u + v)
f = u * v / (u + v)
x = -b + √(b2 - 4ac) / 2a
x = (-b + Math.sqrt(b*b - (4*a*c))) / (2 * a)
y = 2(lb + bh + lh)
y = 2 * (l * b + b * h + l * h)
c = a2 + b2
c = a * a + b * b
z = x3 + y3 - xy / z
z = x * x * x + y * y * y - x * y / z
Write short answers
What is an operator?
An operator is a symbol or sign used to specify an operation to be performed in Java programming.
What are the three main types of operators? Name them.
Three main types of operators are Arithmetical, Logical and Relational.
What is a Truth Table?
A Truth Table is a tabular representation of the truth values of different propositions and the final conclusion drawn after connecting them with the help of some connectives.
What do you understand by Bitwise operator? Explain.
Operators which perform operations on bit level of the operands are referred to as Bitwise Operators.
Explain with an example of each of the following operators:
(a) Arithmetic 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.
int a = 8;
int b = 10;
boolean c = a < b;
a is less than
b so the result of
a < b is
true. Hence, boolean variable
(c) Logical operator
Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value.
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
false boolean values and gives a resultant boolean value as
false. So, boolean variable
(d) Ternary operator
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.
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.
Distinguish between :
(a) Unary and Binary arithmetic operator
|Unary Arithmetic Operator||Binary Arithmetic Operator|
|It operates on a single operand||It operates on two operands|
|Increment (++) and Decrement (--) operators are examples of Unary Arithmetic Operators||Multiplication (*) and Division (/) are examples of Binary Arithmetic Operators|
(b) Increment and Decrement operator
|Increment operator||Decrement operator|
|It increases the value of its operand by 1.||It decreases the value of its operand by 1.|
|It represented as ++||It is represented as --|
(c) Prefix and Postfix operator
|Prefix Operator||Postfix Operator|
|It works on the principle of CHANGE-THEN-USE.||It works on the principle of USE-THEN-CHANGE.|
|It is written before the operand.||It is written after the operand.|
After the execution of these two statements, both a and b will have the value of 100.
After the execution of these two statements, a will have the value of 100 and b will have the value of 99.
(d) Operator and Expression
|An operator is a symbol or sign used to specify an operation to be performed.||An expression is a set of variables, constants and operators.|
|Operator works on operands.||Expression is a combination of operators and operands.|
Define the following with the help of with table.
(a) Bitwise AND
This operator results in high (1) if both the operands are high, otherwise low (0).
(b) Bitwise NOT
This operator results in high (1) if bit operand is low (0) and vice-versa. Bitwise NOT is a unary operator.
(c) Bitwise OR
Bitwise OR operator results in high (1), if either or all of its operands are high otherwise low (0).
|a||b||a | b|
(d) Bitwise XOR
This operator result is low (0) for the same values of the operands. The outcome is high (1) for different values.
|a||b||a ^ b|
If m = 5, n =2; what will be the output of m and n after execution?
(a) m -= n
m -= n
⇒ m = m - n
⇒ m = 5 - 2
⇒ m = 3
(b) n = m + m/n;
n = m + m/n
⇒ n = 5 + 5 / 2
⇒ n = 5 + 2
⇒ n = 7
What will be the output when the following statements are executed?
int v,s,n=550; s = n + v > 1750? 400:200;
(a) v = 500
n + v = 500 + 550 = 1050. As 1050 is less than 1750 so condition of ternary operator is false. Ternary operator returns 200 as its output that gets assigned to s.
(b) v = 1500
n + v = 1500 + 550 = 2000. As 2000 is greater than 1750 so condition of ternary operator is true. Ternary operator returns 400 as its output that gets assigned to s.
If a = 0, b = 30, c = 40; then find the value of 'a' when:
a += --b + c++ + b;
a = 98
a = --b + c++ + b
⇒ a = 29 + 40 + 29
⇒ a = 98