Answers To Worksheet - 07. Conditional Constructs in Java
Answers To Worksheet - 07. Conditional Constructs in Java
2. Explain, with the help of an example, the purpose of default in a switch. [2] – 2005
Ans: The default statement in a switch statement is used to execute a set of statements, if no
matching case is found. The default clause must be the last block, as it executes when none
of the previous case values are matching.
Example:
switch(choice)
{
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
:
:
default:
System.out.println(“Invalid day”);
break;
}
10. Rewrite the following program segment using if…else statements instead of the ternary
operator:
String grade = (mark>=90) ? “A” : (mark>=80) ? “B” : “C”; [2] – 2014
Ans: String grade;
if(mark >= 90)
grade = “A”;
else if(mark >= 80)
grade = “B”;
else
grade = “C”;
14. Name the method, which terminates the entire program from any stage. [1] – 2020
Ans: exit( )
15. True or False: System.exit(0) terminates the program from any point. [1] – 2022
Ans: True
===========================