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
Write a program using switch case to find the volume of a cube, a sphere and a cuboid.
For an incorrect choice, an appropriate error message should be displayed.- Volume of a cube = s * s *s
- Volume of a sphere = (4/3) * π * r * r * r (π = (22/7))
- Volume of a cuboid = l*b*h
Differentiate between if else if and switch-case statements
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");
The relative velocity of two trains travelling in opposite directions is calculated by adding their velocities. In case, the trains are travelling in the same direction, the relative velocity is the difference between their velocities. Write a program to input the velocities and length of the trains. Write a menu driven program to calculate the relative velocities and the time taken to cross each other.