1. #include <stdio.h> 4. #include <stdio.
h>
int main() int main()
{ {
int i=3; int i=3;
switch(i) switch(i)
{ {
case 1: case 1:
printf("DO\n"); printf("DO\n");
case 2: case 2:
printf("Re\n"); printf("Re\n");
case 3: break;
printf("Me\n"); case 3:
default: printf("Me\n");
printf("Sa Re Ga Ma Pa\n"); continue;
} default:
return 0; printf("Sa Re Ga Ma Pa\n");
} }
Ans:- Me return 0;
Sa Re Ga Ma Pa }
Ans:- Error(continue is not used in switch)
2. #include <stdio.h>
int main() 5. #include <stdio.h>
{ int main()
int i = 0; {
switch (i) char i=3;
{ switch(i)
case '0': printf("Geeks"); {
break; case '1':
case '1': printf("Quiz"); printf("DO\n");
break; case '2':
default: printf("GeeksQuiz"); printf("Re\n");
} case '3':
return 0; printf("Me\n");
} default:
printf("Sa Re Ga Ma Pa\n");
Ans:-GeeksQuiz }
printf("India");
3. #include <stdio.h> return 0;
int main() }
{
int i = 3; Ans:- Sa Re Ga Ma Pa
switch (i) India
{
case 0+1: printf("Geeks"); 6. #include <stdio.h>
break; int main()
case 1+2: printf("Quiz"); {
break; int k=-2,j=4;
default: printf("GeeksQuiz"); switch(k/=j/k)
} {
return 0; default:
} printf("all are same\n");
Ans:- Quiz case 0:
printf("Happy birthday\n");
case 1: {
printf("A punch on the mouth\n");
case 2: case 2:
printf("A kick in the back\n"); printf("0");
} continue;
return 0; case 3:
} break;
case 4:
Ans:- A punch on the mouth case 5:
A kick in the back printf("H");
break;
7. #include <stdio.h> default:
int main() printf("!\n");
{ }}
int j,x=0; return 0;
for(j=0;j<=5;j++) }
{
switch(j-1) Ans:- 0HH!
{ !
!
case 0: !
case -1: !
x+=1;
break; 9. #include <stdio.h>
case 1: int main()
case 2: {
case 3: char ch='E';
x+=2; switch(ch)
break; {
default: case (ch>=65 &&ch<=90):
x+=3; printf("Capital letter\n");
} break;
printf("x= %d\n",x); case (ch>=97 &&ch<=122):
} printf("Small letter\n");
break;
return 0; case (ch>=48 &&ch<=57):
} printf("Digits\n");
break;
Ans:- x= 1 default:
x= 2 printf("Any other character\n");
x= 4 }
x= 6
x= 8 return 0;
x= 11 }
Ans:- Error(case label does not reduce to an
8. #include <stdio.h> integer constant)
int main()
{ 10. #include <stdio.h>
int j; int main()
for(j=2;j<=10;j++) {
{ int i,j;
switch(j) for(j=1;j<=10;j++)
{ printf("computer\n");
for(i=1;i<=10;i++) printf("C programming\n");
{ }
if(j<10)
goto there; return 0;
} }
printf("I am out of home\n");
Ans:-computer
printf("I am out of city\n");
C programming
printf("I am out of india\n");
}
13. #include <stdio.h>
there: int main()
printf(" I love india\n"); {
int i,k=1;
return 0; here:
} if(k>2)
Ans:- I love india goto out;
there:
11. #include <stdio.h> for(i=1;i<=5;i++)
int main() printf("%d\n",i);
{ k++;
int i; goto here;
for(i=1;i<=5;i++) out:;
{ return 0;
if(i*i>121) }
goto there;
else Ans:-1
printf("%d\n",i); 2
} 3
there: 4
printf(" No more murphy's laws\n"); 5
1
return 0; 2
} 3
4
Ans:-1 5
2
3
4 14. #include <stdio.h>
5 int main()
No more murphy's laws {
int i=1;
switch(i)
12. #include <stdio.h> {
int main() case 1:
{ goto label;
int i,j,k; label:
for(j=1;j<=4;j++) case 2:
{ printf("he looks like a saint...\n");
if(j*j==16) break;
goto anand; }
} printf("A beautiful bird\n");
for(i=1;i<=5;i++) return 0;
{ }
k=i*i;
j=k+2;
anand:
Ans:-he looks like a saint... main()
A beautiful bird {
char ch='A';
15. #include<stdio.h> switch(ch)
main() {
{ case 'A':
int expr=10; printf("Case label is A");
switch(expr)
printf("This is valid but will not get case "B":
executed"); printf("Case label is B");
} }
Ans:- No output }
Ans: error: case label”B” does not
16. #include<stdio.h> reduce to an integer constant
main()
{ 20. #include<stdio.h>
int expr=10; main()
switch(expr); {
printf("Tell whether this will get int expr=1;
executed or not"); switch(expr)
} {
Ans: Tell whether this will get case 1:printf("One\n");
executed or not case 2:printf("Two\n");
default:printf("Three\n");
17. #include<stdio.h> }
main() }
{ Ans: One
float expr=2.0; Two
switch(expr) Three
{
case 1:printf("One"); 21. #include<stdio.h>
case 2:printf("Two"); main()
default:printf("default"); {
} int expr=1;
} switch(expr)
Ans: error: switch quantity not an {
integer case 1:
printf("One\n");
18. #include<stdio.h> break;
main() case 2:
{ printf("Two\n");
int expr=2,j=1; break;
switch(expr) default:
{ printf("Three\n");
case j: }
printf("This is case 1"); }
case 2: Ans :One
printf("This is case 2");
default: 22. #include<stdio.h>
printf("This is default case"); main()
} {
} int expr=3;
Ans: error: case label does not reduce switch(expr)
to an integer constant {
default:printf("Three\n");
case 1:printf("One\n");
19. #include<stdio.h> case 2:printf("Two\n");
} case 5:
} i += 5;
ANS: Three default:
One i += 4;
Two break;
}
23. #include<stdio.h> printf("%d ", i);
main() }
{ return 0;
int expr=2; }
switch(expr)
{ Ans:- 16 21
case 1:
printf("This is case 1");
case 2-1: 26. #include<stdio.h>
printf("This is case 2"); void main()
} {
} char inchar = 'A';
Ans: error: duplicate case value switch (inchar)
{
24. #include<stdio.h> case 'A' :
main() printf ("choice A \n") ;
{ case 'B' :
int i=1,j=3; printf ("choice B \n ") ;
switch(i) case 'C' :
{ case 'D' :
case 1: case 'E' :
printf("This is outer case 1\n"); default:
switch(j) printf ("No Choice") ;
{ }
case 3: }
printf("This is inner case 1\n");
break; Ans:- choice A
default: choice B
printf("This in inner default case"); No Choice
}
case 2: 27. #include <stdio.h>
printf("This is outer case 2"); int main()
} {
} int i = 3;
Ans: This is outer case 1 switch(i)
This is inner case 1 {
This is outer case 2 printf("Outside ");
case 1: printf("Geeks");
break;
25. # include <stdio.h> case 2: printf("Quiz");
int main() break;
{ defau1t: printf("GeeksQuiz");
int i = 0; }
for (i=0; i<20; i++) return 0;
{ }
switch(i)
{ Ans:- No output(default have 1 not
case 0: L)
i += 5;
case 1: 28. #include <stdio.h>
i += 2; int main()
{ staticint size=0, stkTop=0;
char check = 'a'; switch (opcode)
switch (check) {
{ case -1:
case 'a' || 1: printf("Geeks "); size = val;
break;
case 'b' || 2: printf("Quiz "); case 0:
break; if (stkTop< size ) A[stkTop++]=val;
default: printf("GeeksQuiz"); break;
} default:
return 0; if (stkTop) return A[--stkTop];
} }
return -1;
}
Ans:- Compilation Error int main()
{
29. #include <stdio.h> int B[20];
int main() A=B;
{ stkTop = -1;
int check = 20, arr[] = {10, 20, 30}; stkFunc (-1, 10);
switch (check) stkFunc (0, 5);
{ stkFunc (0, 10);
case arr[0]: printf("Geeks "); printf ("%d\n", stkFunc(1, 0)+
case arr[1]: printf("Quiz "); stkFunc(1, 0));
case arr[2]: printf("GeeksQuiz"); }
} Ans:- 15
return 0;
}
Ans:- Compilation Error
30. #include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
default:
a = 4;
case 6:
a--;
case 5:
a = a+1;
case 1:
a = a-1;
}
printf("%d n", a);
return 0;
}
Ans:- 5
31. #include <stdio.h>
int *A, stkTop;
intstkFunc (intopcode, intval)
{