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
An air-conditioned bus charges fare from the passengers based on the distance travelled as per the tariff given below:
Distance Travelled Fare Up to 10 km Fixed charge ₹80 11 km to 20 km ₹6/km 21 km to 30 km ₹5/km 31 km and above ₹4/km Design a program to input distance travelled by the passenger. Calculate and display the fare to be paid.
Which of the following is not true with regards to a switch statement?
- checks for an equality between the input and the case labels
- supports floating point constants
- break is used to exit from the switch block
- case labels are unique
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");
The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return