Chapter 4 Operators in Java
Chapter 4 Operators in Java
Operators in Java
Class 10 - APC Understanding Computer Applications
with BlueJ
Question 1
1. !, &&, ||
2. &&, !, ||
3. ||, !, &&
4. &&, ||, !
Answer
!, &&, ||
Question 2
1. >
2. ==
3. &&
4. <
Answer
&&
Reason — && is the logical AND whereas the other operators are relational.
Question 3
1. Best
2. Excellent: Best
3. Best: Excellent
4. Excellent
Answer
Excellent
Reason — Since condition (a>= 90) of ternary operator is true, st is assigned the value of expression 1 i.e.,
excellent.
Question 4
1. Unary operator
2. Binary operator
3. Ternary operator
4. Bitwise operator
Answer
Bitwise operator
Reason — Unary operator, Binary operator and ternary operator are arithmetic operators.
Question 5
1. 23
2. 21
3. 22
4. 24
Answer
23
Question 1
Question 2
Question 3
The relational operators always result in terms of 'True' or 'False'.
True
Question 4
Given: int m=5; m*=5 then the value stored in m results in 55.
False
Question 5
Question 6
Question 7
Question 8
Question 1
ab+cd33ab+cd
Answer
Math.cbrt(a * b + c * d)
Question 2
p3+q4−12rp3+q4−21r
Answer
Math.pow(p, 3) + Math.pow(q, 4) - (1.0 / 2.0 * r)
Question 3
−b+b2−4ac2a2a−b+b2−4ac
Answer
(-b + Math.sqrt((b * b) - (4 * a * c))) / (2.0 * a)
Question 4
0.05−2y3(x−y)(x−y)0.05−2y3
Answer
(0.05 - (2 * Math.pow(y,3))) / (x - y)
Question 5
mn+(m+n)3mn+3(m+n)
Answer
Math.sqrt(m * n) + Math.cbrt(m + n)
Question 6
34(a+b)−25ab43(a+b)−52ab
Answer
(3.0 / 4.0 * (a + b)) - (2.0 / 5.0 * a * b)
Question 7
38(b2+c3)83(b2+c3)
Answer
3.0 / 8.0 * Math.sqrt((Math.pow(b,2) + Math.pow(c,3)))
Question 8
a3+b2−c33a+b2−3c
Answer
Math.cbrt(a) + Math.pow(b,2) - Math.cbrt(c)
Question 9
a+b2+c333a+b2+c3
Answer
Math.sqrt(a + Math.pow(b,2) + Math.pow(c,3)) / 3
Question 10
3x+x2(a+b)(a+b)3x+x2
Answer
Math.sqrt(3 * x + Math.pow(x,2)) / (a + b)
Question 11
z=x3+y3−yz3z=x3+y3−z3y
Answer
z = Math.pow(x,3) + Math.pow(y,3) - y / Math.pow(z,3)
Question 12
q=1a+b+3c2q=a+b1+c23
Answer
q = 1 / Math.sqrt(a + b) + 3 / Math.pow(c,2)
Predict the output
Question 1
Output
12
Explanation
As 3 is less than 4 so condition of ternary operator is true. Variable c is assigned the value of expression 1 which is 3
* 4 = 12.
Question 2
Output
true
Explanation
As 14 is greater than 4 so condition of ternary operator is true. Variable x is assigned the value of expression 1
which is true.
Question 3
int x = 90;
char c = (x<=90)? 'Z' : 'I';
Output
Explanation
As value of x is 90 so condition of ternary operator is true. Variable c is assigned the value of expression 1 which is
Z.
Question 4
int a = 18; int b = 12;
boolean t = (a > 20 && b < 15)? true : false;
Output
false
Explanation
The condition a > 20 is false as value of a is 18. So the logical AND operator — && returns false. Variable t is
assigned the value of expression 2 which is false.
Question 5
Output
(a) 200
(b) 400
Explanation
When val = 1000, val + 550 = 1550. As 1550 is less than 1700 so condition of ternary operator is true. Variable c is
assigned the value of expression 1 which is 200.
When val = 1500, val + 550 = 2050. As 2050 is greater than 1700 so condition of ternary operator is false. Variable
c is assigned the value of expression 2 which is 400.
Question 1
What is an operator? What are the three main types of operators? Name them.
Answer
The three main types of operators are Arithmetical, Logical and Relational.
Question 2
Answer
An expression is a set of variables, constants and operators i.e. an expression is a combination of operators and
operands. When an expression is assigned to a variable, the complete set is referred to as a statement.
Question 3
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
Question 4
Distinguish between:
Answer
Increment (++) and Decrement (--) operators are examples Multiplication (*) and Division (/) are examples of
of Unary Arithmetic Operators Binary Arithmetic Operators
Answer
Example: Example:
int a = 99; int a = 99;
int b = a++; int b = ++a;
After the execution of these two statements, a will have the After the execution of these two statements, both
value of 100 and b will have the value of 99. a and b will have the value of 100.
Answer
Example: Example:
int a = 100; int a = 100;
int b = a--; int b = --a;
After the execution of these two statements, a will have the After the execution of these two statements, both
value of 99 and b will have the value of 100. a and b will have the value of 99.
Answer
(p != q) !(p == q)
This expression uses the relational This expression first checks if values of p and q are equal using the
(p != q) !(p == q)
operator != (Not equal to) to relational operator == (equality). It then inverts the result of equality
determine if values of p and q are operator using the logical NOT (!) operator to determine if values of p
different. and q are different.
Question 5
Answer
/ %
Returns the quotient of division operation Returns the remainder of division operation
Example: int a = 5 / 2; Here a will get the value of 2 Example: int b = 5 % 2; Here b will get the value of 1
which is the quotient of this division operation which is the remainder of this division operation
(b) = and == ?
Answer
= ==
It is the assignment operator used for assigning a It is the equality operator used to check if a variable is equal to
value to a variable. another variable or literal.
Example: Example:
int a = 10; if (a == 10)
This statement assigns 10 to variable a. This statement checks if variable a is equal to 10 or not.
Question 6(a)
int k=5,j=9;
k += k++ - ++j + k;
System.out.println("k="+k);
System.out.println("j="+j);
Output
k=6
j=10
Explanation
⇒ k = k + (k++ - ++j + k)
k+= k++ - ++j + k
⇒ k = 5 + (5 - 10 + 6)
⇒k=5+1
⇒k=6
Question 6(b)
Output
z = 176
Explanation
⇒ z = (11 * 16)
⇒ z = 176
Question 6(c)
Output
a = 39
Explanation
⇒ a = 7 + (7 + 9 + 8 + 8)
⇒ a = 7 + 32
⇒ a = 39
Question 6(d)
Output
y = 33
Explanation
⇒ y = 8 + (9 + 9 + 7)
⇒ y = 8 + 25
⇒ y = 33
Question 7
Rewrite the following program segment using if-else statements instead of the ternary operator.
Answer
String grade;
if (marks >= 90)
grade = "A";
else if (marks >= 80)
grade = "B";
else
grade = "C";
(b) commission = (sale > 5000) ? sale*10/100 : 0;
Answer
Answer
Answer
Answer
Question 8
(a)
if (x % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
Answer
System.out.println(x % 2 == 0 ? "Even" : "Odd");
(b)
if(a > b)
{
if (a > c)
g = a;
else
g = c;
}
else if (b > c)
g = b;
else
g = c;
Answer
if (p >= 4750)
k = p * 5 / 100;
else
k = p * 10 / 100;
Answer