14. Rewrite the following using ternary operator.
(a) if(income<=100000)
tax =0;
else
tax = (0.1*income);
(b) if(p>5000)
d = p*5/100;
else
d = p*2/100;
Answers
Answer:
d. None; What is the result of the following in Java statement, ... c. 55 d. None; int m, p; m = 5; p = 0; p = m-- + --m; The output will be: a. ... The ternary operator deals with three operands .
Answer:
Multiple Choice Questions
TICK THE CORRECT ANSWER:
The statement n += 4; is equivalent to:
a. ++n;
b. n = n + 4;
c. n + 1;
d. None
What will be the output of a and b in the following:
int a, b;
a = 10;
b = a++;
a. 10, 10
b. 10, 11
c. 11, 10
d. 11, 11
What will be the output of a++; when int a = -1;
a. 1
b. -1
c. 0
d. None
If int a = 25, b = 5, c = 0; What value is stored in c, when c = a % b;?
a. 5.0
b. 5
c. 0
d. None
What is the result of the following in Java statement, when
int m = 8;
m *= 8;
System.out.println("The output = " + m);
a. 8
b. 64
c. 16
d. 88
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
int m, p;
m = 5;
p = 0;
p = m-- + --m;
The output will be:
a. 11
b. 10
c. 8
d. 12
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
WRITE THE JAVA EXPRESSIONS FOR THE FOLLOWING:
p = a2 + bc
p = a * a + b * c;
m = (a2 – b2) ÷ ab
m = (a * a - b * b) / (a * b);
s = ut + ½ at2
s = u * t + 1.0 / 2 * a * t * t;
f = (uv) ÷ (u + v)
f = (u * v) / (u + v);
(a + b)2 + b
(a + b) * (a + b) + b;
y = 2(lb + bh + lh)
y = 2 * (l * b + b * h + l * h);
a2 + b2
a * a + b * b;
z = x3 + y3 – xy ÷ 3
z = x * x * x + y * y + y - x * y / 3;
ANSWER THE FOLLOWING QUESTIONS:
What is an operator?
An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result.
Name the different types of operators.
The different types of operators are:
a. Unary
b. Binary
c. Ternary.
Explain the following:
a. Arithmetical operator: The operators used to perform arithmetical calculations in a program are known as arithmetical operators.
b. Relational operator: The operators used to show the relationship between the operands by comparing the values of the variables and give the result as true or false, are known as relational operators.
c. Logical operator: AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.
d. Unary operator: The operators applied to a single operand is known as unary operator.
e. new operator: The new operator 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: The operators that deal with two operands is known as binary operator.
What is a ternary operator? Explain with the help of an example.
The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.
Example:
int a = 5:
int b = 3;
int max = (a > b)? a : b;
Differentiate between the following:
a. Arithmetical operator and logical operator: The operators used to perform arithmetical calculations in a program are known as arithmetical operators. AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.
b. Binary operator and Ternary operator: The operators that deal with two operands is known as binary operator. The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.
c. Logical AND and Logical OR: Logical AND combines two or more boolean expressions in such a way that it results in true if all the expressions are true, otherwise it returns false. Logical OR combines two or more boolean expressions in such a way that it will return true when any one condition is true, otherwise it returns false.
d. Prefix operator and Postfix operator: Prefix means that the value of the variable changes before the operation takes place. Postfix means that the operand will change after performing the operation.
e. System.out.print() and System.out.println(): When we use print(), the cursor remains in the same line after producing the result. But in println(), the cursor shifts to the next line after producing the result.
Differentiate between an operator and an expression.
An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result. An expression is any meaningful combination of variables, constants and/or operators, that, when calculated, yields a value.
If m = 5 and n = 2, then what will be the output of m and n after execution that will store in (a) and (b)?
a. m -= n;
m = m – n;
= 5 – 2
= 3
b. n = m + m / n;
n = 5 + 5 / 2
= 5 + 2
= 7
State the difference between = and ==.
= is an assignment operator. It assigns a value to a variable. == is a relational operator. It compares two values for equality.
What will be the output of the following program segment?
int a = 0, b = 10, c = 40;
a = --b + c++ + b;
System.out.println("a = " + a);
a = 9 + 40 + 9
= 58.
What will be the output of the following, if x = 5;?
a. 5 * ++x
= 5 * 6
= 30.
b. 5 * x++
= 5 * 5
= 25.
Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
a. a - (b++) * (--c)
= 2 – 3 * 8
= 2 – 24
= -22.
b. a * (++b) % c
= 2 * 4 % 9
= 2 * ;