Computer Applications
Which of the following is true about the default
label in a switch
statement?
Answer
It executes if no matching case label is found.
Reason — The default
label in a switch
statement is used as a fallback option. If none of the case
labels match the switch
expression, the default
block executes.
- Including a
default
label is optional in aswitch
statement. - It can appear anywhere within the
switch
block. - The
default
label can appear only once, but its position within theswitch
block is flexible (not restricted to the top).
Related Questions
Ravi runs the following Java code but encounters an error. Identify the statement causing the issue, correct it, and ensure the output is "Hungry".
char h = 'Y'; if (h == 'y' || 'Y') System.out.println("Hungry"); else System.out.println("Not Hungry");
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");
Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise, display they are equal. The program also displays whether the numbers entered by the user are 'All positive', 'All negative' or 'Mixed numbers'.
Sample Input: 56, -15, 12
Sample Output:
The greatest number is 56
Entered numbers are mixed numbers.Write a Java program to print name, purchase amount and final payable amount after discount as per given table:
Purchase Amount Discount upto ₹10000/- 15% ₹10000 to ₹ 20000/- 20% Above ₹20000/- 30%