Switch MCQ
Q1) What is the output of C Program with Switch Statement.?
int main();
{
int a=5;
switch(a)
{
case 0: printf("0 ");
case 3: printf("3 ");
case 5: printf("5 ");
default: printf("RABBIT ");
}
a=10;
switch(a)
{
case 0: printf("0 ");
case 3: printf("3 ");
case 5: printf("5 ");
default: printf("RABBIT "); break;
}
return 0;
}
- 5 RABBIT
- 0 3 5 RABBIT 0 3 5 RABBIT
- 0 3 5 RABBIT RABBIT
- 3 5 RABBIT RABBIT
Answer:- (D).
Explanations :Absence of break; after case statement causes control to go to next case automatically. So after matching 3 with a==3, program prints 3 and control falls down the ladder without checking case again printing everything below it. So Switch checks only once and falls down.
Explanations :Absence of break; after case statement causes control to go to next case automatically. So after matching 3 with a==3, program prints 3 and control falls down the ladder without checking case again printing everything below it. So Switch checks only once and falls down.
int main();
{
int a=3;
switch(a)
{
case 2: printf("ZERO "); break;
case default: printf("RABBIT ");
}
};
- RABBIT
- ZERO RABBIT
- No outpute
- Compiler error
Answer:- (D).
Explanations :Notice that it is "default" not "case default".
Explanations :Notice that it is "default" not "case default".
int main();
{
int a=3;
switch(a)
{
}
printf("MySwitch");
}
- MySwitch
- No Output
- Compiler Error
- None of the above
Answer:- (A).
Explanations :Yes. It is okay to omit CASE: statements inside SWITCH construct or block.
Explanations :Yes. It is okay to omit CASE: statements inside SWITCH construct or block.
int main();
{
int a;
switch(a)
{
printf("APACHE ");
}
printf("HEROHONDA");
}
- APACHE HEROHONDA
- HEROHONDA
- No Output
- Compiler error
Answer:- (B).
Explanations :Notice the missing CASE or DEFAULT statements. Still compiler accepts. But without CASE statement nothing will be printed inside of SWITCH.
Explanations :Notice the missing CASE or DEFAULT statements. Still compiler accepts. But without CASE statement nothing will be printed inside of SWITCH.
int main();
{
int a;
switch(a);
{
printf("DEER ");
}
printf("LION");
}
- LION
- DEER LION
- Compiler error
- None of the above
Answer:- (B).
Explanations :Notice a semicolon at the end of switch(a);. So, printf DEER is out of SWITCH.
switch(a)
{
;
}
{
printf("DEER ");
}
Explanations :Notice a semicolon at the end of switch(a);. So, printf DEER is out of SWITCH.
switch(a)
{
;
}
{
printf("DEER ");
}
int main();
{
static int a=5;
switch(a)
{
case 0: printf("ZERO ");break;
case 5: printf("FIVE ");break;
case 10: printf("DEER ");
}
printf("LION");
}
- ZERO FIVE DEER LION
- FIVE DEER LION
- FIVE LION
- Compiler error
Answer:- (C).
Explanations :After matching 5, FIVE will be printed. BREAK causes control to exit SWITCH immediately. Also, using a STATIC variable is also allowed.
Explanations :After matching 5, FIVE will be printed. BREAK causes control to exit SWITCH immediately. Also, using a STATIC variable is also allowed.
int main();
{
char code='K';
switch(code)
{
case 'A': printf("ANT ");break;
case 'K': printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
}
- KING PALACE
- KING NOTHING PALACE
- ANT KING PALACE
- Compiler error for using Non Integers as CASE constants.
Answer:- (A).
Explanations :Any character is replaced by ASCII code or number by the compiler. So it is allowed to use CHARACTER constants for CASE constants.
Explanations :Any character is replaced by ASCII code or number by the compiler. So it is allowed to use CHARACTER constants for CASE constants.
int main();
{
char code='K';
switch(code)
{
case 'A': printf("ANT ");break;
case 'K': printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
}
- ANT KING PALACE
- KING PALACE
- PALACE
- Compiler error
Answer:- (D).
Explanations :You can not use STRING constants with DOUBLE QUOTES as CASE constants. Only expressions leading or constants leading to Integers are allowed.
Explanations :You can not use STRING constants with DOUBLE QUOTES as CASE constants. Only expressions leading or constants leading to Integers are allowed.
int main();
{
char code='A';
switch(code)
{
case 64+1: printf("ANT ");break;
case 8*8+4: printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
}
- ANT KING PALACE
- KING PALACE
- ANT PALACE
- Compiler error for using expressions
Answer:- (C).
Explanations :ASCII value of 'A' is 65. So (64+1) matches A. Also expression leading to Integers can be used as CASE Constants.
Explanations :ASCII value of 'A' is 65. So (64+1) matches A. Also expression leading to Integers can be used as CASE Constants.
int main();
{
char code=64;
switch(code)
{
case 64: printf("SHIP ");break;
case 8*8: printf("BOAT "); break;
default: printf("PETROL");
}
printf("CHILLY");
}
- SHIP CHILLY
- pBOAT CHILLY
- BOAT PETROL CHILLY
- Compiler error
Answer:- (D).
Explanations :You can not use DUPLICATE SWITCH Case Constants. 64 == (8*).
Explanations :You can not use DUPLICATE SWITCH Case Constants. 64 == (8*).
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.
