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
defaultlabel is optional in aswitchstatement. - It can appear anywhere within the
switchblock. - The
defaultlabel can appear only once, but its position within theswitchblock is flexible (not restricted to the top).
Related Questions
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% A student executes the following program segment and gets an error. Identify the statement which has an error, correct the same to get the output as WIN.
boolean x = true; switch(x) { case 1: System.out.println("WIN"); break; case 2: System.out.println("LOOSE"); }The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return
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.