Correct Answer - Option 4 : Goodbye1Goodybye2
Concept:
A switch statement tests the value of a variable and compares it with multiple cases.
Once the case match is found, a block of statements associated with that particular case is executed.
A switch-case statement can accept int, char, long datatypes.
Syntax:
switch(expression)
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Calculation:
main ()
{
int ch = 1;
switch (ch)
{
case 1:
printf ("\nGoodbye1");
case 2:
printf ("\nGoodbye2");
}
}
1. In the given program ch = 1;
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
2. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
As there is no break statement after case 1, case 2 will also be executed.
3. Hence in the output, we will get:
Goodbye1
Goodybye2
Hence option (4) is the correct answer.