k=10, m=5, r=20
k=(--m%5)*(m++*(10+++r)
Answers
Answer:
Question 4
What is a Ternary operator? Explain with the help of an example.
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 5
Differentiate between the following:
(a) Arithmetical operator and Logical operator
Arithmetical Operator Logical Operator
Arithmetic operators are used to perform mathematical operations. Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value.
+, -, *, /, etc. are a few examples of Arithmetic operators. &&, ||, ! are a few examples of Logical Operators
(b) Binary operator and Ternary operator
Binary operator Ternary operator
Binary operators work on two operands. Ternary operator work on three operands.
+, -, *, /, etc. are a few examples of Binary operators. The conditional operator ? : is a Ternary operator.
(c) Logical AND (&&) and Logical OR(||)
Logical AND (&&) Logical OR(||)
It evaluates to true only if both of its operands are true. It evaluates to true if one or both of its operands are true.
Example:
int a = 8, b = 13, c = 0;
if (a > 10 && b > 10)
c = 10;
else
c = 5;
Here, value of c will be 5 as one of the operands is false. Example:
int a = 8, b = 13, c = 0;
if (a > 10 || b > 10)
c = 10;
else
c = 5;
Here, value of c will be 10 as at least one of the operands is true.
(d) Prefix operator 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.
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.
(e) System.out.print( ) and System.out.println( )
System.out.print( ) System.out.println( )
It prints data to the console but the cursor remains at the end of the data in the same line. It prints data to the console and places the cursor in the next line.
Next printing takes place from the same line. Next printing takes place from next line.
Question 6
Differentiate between an operator and an expression.
An operator is a symbol or sign used to specify an operation to be performed whereas an expression is a set of variables, constants and operators i.e. an expression is a combination of operators and operands.
Question 7
If m=5 and n=2 then what will be the output of m and n after execution that will store in (a) & (b)?
(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 8
State the difference between = and ==.
= ==
It is the assignment operator used for assigning a value to a variable. It is the equality operator used to check if a variable is equal to another variable or literal.
E.g. int a = 10; assigns 10 to variable a. E.g. if (a == 10) checks if variable a is equal to 10 or not.
Question 9
What will be the output for the following program segment?
int a=0,b=10,c=40;
a = --b + c++ +b;
System.out.println(" a = " + a);
Output
a = 58
Explanation
a = --b + c++ + b
⇒ a = 9 + 40 + 9
⇒ a = 58
Question 10
What will be the output of the following?
if x =5
(a) 5* ++x;
Answer
5 * ++x
⇒ 5 * 6
⇒ 30
(b) 5* x++;
Answer
5 * x++
⇒ 5 * 5
⇒ 25
Question 11
Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
(a) a - (b++) * (--c);
Answer
a - (b++) * (--c)
⇒ 2 - 3 * 8
⇒ 2 - 3 * 8
⇒ 2 - 24
⇒ -22
(b) a * (++b) % c;
Answer
a * (++b) % c
⇒ a * (++b) % c
⇒ 2 * (4) % 9
⇒ 8 % 9
⇒ 8
Question 12
If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a;
Answer
a += a++ - ++b + a
⇒ a = a + (a++ - ++b + a)
⇒ a = 5 + (5 - 10 + 6)
⇒ a = 5 + 1
⇒ a = 6