Page 191 - CA_Blue( J )_Class9
P. 191
case 1: System.out.println("Sum " + (a+b));
break;
case 2: System.out.println("Difference " +(a-b));
break;
default : System.out.println("Product " +(a*b));
}
4. Rewrite the following program segment using the if..else statement:
comm = (sale > 15000)? sale * 5 / 100 : 0;
Ans. if(sale > 15000)
comm = sale * 5 / 100;
else
comm = 0;
5. Find the error 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");
Ans. 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");
6. Evaluate the value of n if value of p = 5, q = 19.
int n = (q - p) > (p - q)? (q - p) : (p - q);
Ans. 14 [Explanation: n = (19 – 5)>(5-19) = 14>-14 which is true.]
Unsolved Questions
A. Tick ( ) the correct answer.
1. Is it necessary to enclose the code block of an if statement within braces { }?
a. Yes, always
b. No, only if there is more than one line of code in the block
c. No, braces are never required
d. Yes, but only for if-else statements
2. In Java, if statement is a ………………… statement.
a. Boolean b. Conditional
c. Iterative d. Optional
3. Choose the correct syntax of Java if statement below:
a. if(condition)
//statement;
b. if(condition);
{
//statement;
}
Conditional Constructs in Java 189

