Predict the output (switch case problems)
Following are the problems, whose outputs you should be able to guess without running these code snippets on the computer. Good knowledge of "switch and case" statements is necessary for solving these.
These problems help you know where you stand...
//1
main()
{
char suite=3;
switch (suite)
{
case 1:
printf("\nDiamond");
case 2:
printf("\nSpade");
default:
printf("\nHeart");
}
printf("\nI thought one wears a suite.");
}
//2
main()
{
int k, j=2;
switch(k=j+1)
{
case 0:
printf("\nTailor");
case 1:
printf("\nTutor");
case 2:
printf("\nTramp");
default:
printf("Pure simple Egghead!");
}
}
//3
main()
{
int i=0;
switch (i)
{
case 0:
printf("\nTemple is a non-issue");
case 1:
printf("\nAandhi is never stable");
case 2:
printf("\nMandal will ruin India");
case 3:
printf("\nWe want better politicians");
}
}
//4
main()
{
char ch='a';
switch (ch)
{
case 'a':
case 'b':
printf("\nYou entered b");
case 'A':
printf("\na as in ashar");
}
}
main()
{
char i='1';
switch (i)
{
case 0:
printf("\nFeeding Fish");
case 1:
printf("\nWeeding Grass");
case 2:
printf("\nmending roof");
default:
printf("\nJust to survive");
}
}
Look down for answers.....
//ANSWERS:
//1. Heart
// I thought one wears a suite.
//2. Pure Simple Egghead!
//3. Temple is a non-issue
//Aandhi is never stable
//Mandal will ruin India
//We want better politicians
//4. You entered b.
// a as in ashar.
//5. Just to survive.
The File can be downloaded at:
Download File
These problems help you know where you stand...
//1
main()
{
char suite=3;
switch (suite)
{
case 1:
printf("\nDiamond");
case 2:
printf("\nSpade");
default:
printf("\nHeart");
}
printf("\nI thought one wears a suite.");
}
//2
main()
{
int k, j=2;
switch(k=j+1)
{
case 0:
printf("\nTailor");
case 1:
printf("\nTutor");
case 2:
printf("\nTramp");
default:
printf("Pure simple Egghead!");
}
}
//3
main()
{
int i=0;
switch (i)
{
case 0:
printf("\nTemple is a non-issue");
case 1:
printf("\nAandhi is never stable");
case 2:
printf("\nMandal will ruin India");
case 3:
printf("\nWe want better politicians");
}
}
//4
main()
{
char ch='a';
switch (ch)
{
case 'a':
case 'b':
printf("\nYou entered b");
case 'A':
printf("\na as in ashar");
}
}
main()
{
char i='1';
switch (i)
{
case 0:
printf("\nFeeding Fish");
case 1:
printf("\nWeeding Grass");
case 2:
printf("\nmending roof");
default:
printf("\nJust to survive");
}
}
Look down for answers.....
//ANSWERS:
//1. Heart
// I thought one wears a suite.
//2. Pure Simple Egghead!
//3. Temple is a non-issue
//Aandhi is never stable
//Mandal will ruin India
//We want better politicians
//4. You entered b.
// a as in ashar.
//5. Just to survive.
The File can be downloaded at:
Download File
Comments