Class 9 ICSE
Ternary operators
please solve and please don't spam please
Answers
Required Answer:-
Given Information:
- char X = 'A', y = 'a' and z = 'B'
Given to find:
- The value of N.
Solution:
Question 1.
GIVEN,
char N =( X > Y) ? X : Y;
Note:- ASCII Value of 'A' is 65 and that of 'a' 97
So, X > Y is false as 65 > 97 is false.
Therefore,
char N = Y
➡ N = 'a' which is our required answer.
Question 2.
GIVEN,
char N = ( X < Z ) ? ++X : ++Z;
Note:- ASCII value of 'A' is 65 and 'B' is 66.
So, X < Y is true as 65 < 66 is true.
Therefore,
char N = ++Z
➡ char N = 'C' (++Z = Character with ASCII value 67(66+1))
Hence,
char n = 'C' which is our required answer.
Question 3.
GIVEN,
boolean N = ( X == 'A') || (X == 'a') ? true : false;
Here, X == 'A' is true but X == 'a' is false.
As, any one of the conditions is satisfied. So,
➡ boolean N = true which is our required answer.
Question 4.
GIVEN,
int N = ( Y >= X ) ? Y : X;
Note:- ASCII value of 'a' is 97 and that of 'A' is 65
So, Y >= X is true.
So,
int n = Y
➡ int n = (ASCII value of 'a')
➡ int n = 97 which is our required answer.
Explanation:
conditionAn expression whose value is used as a condition.exprIfTrueAn expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).exprIfFalseAn expression which is executed if the condition is falsy (that is, has a value which can be converted to false).