Ques 1 SwitchCase
What is the output of the following code?
int main()
{
int k=0;
k=k+2;
switch(k)
{
case 0:printf("case 0");
case 1:printf("case 1");
case 2:printf("case 2");break;
default: printf("SmallCode");
}
return 0;
}
a) case 0 case 1
b) case 1
c) case 2
d) SmallCode
Ques 2 SwitchCase
What is the output of the following code?
int main()
{
int k=0;
k=k+2;
switch(k)
{
case 0:printf("case 0");
case 1:printf("case 1");break;
case 2:printf("case 2");continue;
default: printf("SmallCode");
}
return 0;
}
a) case 0
b) case 1
c) case 2
d) compilation error
Ques 3 SwitchCase
What is the output of the following code?
int main()
{
int k=0;
switch(k)
{
case 0:printf("case 0 ");
case 1:printf("case 1");break;
case 2:printf("case 2");break;
default: printf("SmallCode");
}
return 0;
}
a) case 0 case 1
b) case 1
c) case 2
d) SmallCode
k is initialized to 0, so the switch statement begins with case 0.
Case 0: printf("case 0 "); is executed, printing "case 0 ".
Fall-through: Since there is no break statement after case 0, execution continues to the next case, which is case 1.
Case 1: printf("case 1"); is executed, printing "case 1".
Break: The break after case 1 stops further execution, so case 2 and default are not executed.
The final output is: case 0 case 1.