Operators and Expressions
Fill in the blanks
Question 1
The statement n++ is equivalent to n = n + 1.
Question 2
If a=10;b=++a, then value of a and b will be 11 and 11.
Question 3
If a= -1 , then the output of a++ will be 0.
Question 4
If int a=43;int b=5;int c=0; and c = a%b; then the value of c will be 3.
Question 5
If int m=8; m*=8; then the output of m will be 64.
Question 6
If x=5;y=10;z=11; and c = x++ * 5 + --y * ++z; then the value of c will be 133.
Question 7
If m=5;p=0;and p = ++m + --m; the output will be 11.
Question 8
If int a=7;int p=0; and p = ++a + - -a; the value of p will be 16.
Question 9
The binary equivalent of (45)10 is 101101.
Question 10
If a=0,b=1; then the output of bitwise OR will be 1.
Write the Java statements 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 / 2) * a * t * t
Question 4
f = uv / (u + v)
Answer
f = u * v / (u + v)
Question 5
x = -b + √(b2 - 4ac) / 2a
Answer
x = (-b + Math.sqrt(b*b - (4*a*c))) / (2 * a)
Question 6
y = 2(lb + bh + lh)
Answer
y = 2 * (l * b + b * h + l * h)
Question 7
c = a2 + b2
Answer
c = a * a + b * b
Question 8
z = x3 + y3 - xy / z
Answer
z = x * x * x + y * y * y - x * y / z
Write short answers
Question 1
What is an operator?
Answer
An operator is a symbol or sign used to specify an operation to be performed in Java programming.
Question 2
What are the three main types of operators? Name them.
Answer
Three main types of operators are Arithmetical, Logical and Relational.
Question 3
What is a Truth Table?
Answer
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.
Question 4
What do you understand by Bitwise operator? Explain.
Answer
Operators which perform operations on bit level of the operands are referred to as Bitwise Operators.
Question 5
Explain with an example of each of the following operators:
(a) Arithmetic operator
Answer
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
Answer
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
Answer
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 these true
and false
boolean values and gives a resultant boolean value as false
. So, boolean variable c
becomes false
.
(d) Ternary operator
Answer
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 6
Distinguish between :
(a) Unary and Binary arithmetic operator
Answer
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
Answer
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
Answer
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. |
Example:int a = 99; int b = ++a; After the execution of these two statements, both a and b will have the value of 100. | Example:int a = 99; int b = a++; 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
Answer
Operator | 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. |
Question 7
Define the following with the help of with table.
(a) Bitwise AND
Answer
This operator results in high (1) if both the operands are high, otherwise low (0).
Truth Table
a | b | a&b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
(b) Bitwise NOT
Answer
This operator results in high (1) if bit operand is low (0) and vice-versa. Bitwise NOT is a unary operator.
Truth Table
a | !a |
---|---|
0 | 1 |
1 | 0 |
(c) Bitwise OR
Answer
Bitwise OR operator results in high (1), if either or all of its operands are high otherwise low (0).
Truth Table
a | b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
(d) Bitwise XOR
Answer
This operator result is low (0) for the same values of the operands. The outcome is high (1) for different values.
Truth Table
a | b | a ^ b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Question 8
If m = 5, n =2; what will be the output of m and n after execution?
(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 9
What will be the output when the following statements are executed?
int v,s,n=550;
s = n + v > 1750? 400:200;
When,
(a) v = 500
Output
200
Explanation
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
Output
400
Explanation
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.
Question 10
If a = 0, b = 30, c = 40; then find the value of 'a' when:
a += --b + c++ + b;
Output
a = 98
Explanation
a = --b + c++ + b
⇒ a = 29 + 40 + 29
⇒ a = 98