Page 143 - CA_Blue( J )_Class10
P. 143
C. Answer the following questions:
1. Convert the following to Ternary Operator:
if(a>=4 && b==6)
c=a+b;
else
c=a-b;
Ans. c=(a>=4 && b==6)? a+b : a-b;
2. Convert the following to if-else statement:
double comm = (sales >=20000)? 5.0/100.0*sales : 2.5/100.0*sales;
Ans. double comm;
if(sales>=20000)
comm=5.0/100.0*sales;
else
comm=2.5/100.0*sales;
3. Convert the following to switch case statement:
char grade;
if(grade== 'A')
System.out.println("Marks greater than 90");
else if(grade == 'B')
System.out.println("Marks greater than 70");
else if(grade == 'C')
System.out.println("Marks greater than 50");
else
System.out.println("Marks less than or equal to 50");
Ans. char grade;
switch(grade)
{
case 'A':
System.out.println("Marks greater than 90");
break;
case 'B':
System.out.println("Marks greater than 70");
break;
case 'C':
System.out.println("Marks greater than 50");
break;
default:
System.out.println("Marks less than or equal to 50");
}
4. Find the output:
int a=65, b=66;
System.out.println(((char)a > (char)b) ? (char)(65+32) : (char)(66+32));
Ans. b
ASCII value of a is 97 which is the sum of 65+32. ASCII value of A is 65. Similarly, 66 is the ASCII value of B and 98 is the ASCII
value of b.
5. Find the errors and correct them:
if(a==1)
System.out.println("A is one");
Else if(b==2);
{
system.out.println("B is two");
else
System.out.println("C is three");
141
Conditional Constructs in Java 141

