Switch Case in C Programming Tutorial And MCQ
Table of Contents
Learn Switch Case in C Tutorial and C MCQ Questions and Answers on Switch Case Syntax to attend job placement exams, interview questions, college viva and Lab Tests.
Switch Case in C
C language provides IF, ELSE IF and ELSE conditional statements to suit various programming needs. Using so many if-else statements look clumsy some times. So, C language an alternative to if-else i.e SWITCH CASE.
Keywords used to implement a SWITCH CASE are switch, case, break and default.
Syntax:
switch(integer constant or expression) { case constant1: statement1; case constant2: statement2; default: statement4; }
Usual way of implementing a SWITCH
switch(integer constant) { case constant1: statements1; break; case constant2: statements2; break; case constant3: statements3; break; default: statement4; }
In the above switch control statement, an integer constant is accepted as a constant. You can use any int data type variable or integer expression as INPUT to a switch. Order of switch implementation is explained below.
- Step 1: The input CONSTANT is compared against first case constant i.e constant1. If it matches, corresponding statements are executed.
2. Step 2: If there is no break; after statements, statements in next case constant section are also executed. There also if there is no break, control goes to the next case constant statements and so on. This way of executing all the statements down the ladder are called SWITCH CASE FALL THROUGH.
3. Step 3: Finally, if no case constant is matched with input CONSTANT, default case statements are executed. Usually we keep default case at the end of SWITCH. Placement or order of DEFAULT can be anywhere.
4. Step 4: Switch CASE CONSTANTS can not be Variables.
5. Step 5: There is no effect of CONTINUE statement in a Switch Statement.
6. Step 6: Proper use of break statement at the end of statements ensures no side effects in SWITCH implementation.
Switch Case in C Example
1:
void display(); int main() { int b=8; display(b); display(10); return 9; } void display(int d) { switch(d) { case 6: printf("SIX");break; case 8: printf("EIGHT"); break; case 9: printf("NINE"); break; default: printf("UNKNOWN");break; } printf("\n"); } //output //EIGHT //UNKNOWN
Above program prints a number in text format using switch case.
Example 2:
int main() { switch('c') { case 'a': printf("A");break; case 'b': printf("B"); break; case 'c': printf("C"); break; default: printf("UNKNOWN");break; } } //output //C
You can character constants inside CASE constants. ASCII values of character constants are nothing but integer numbers.
[WpProQuiz 30]
C Programming Switch Case MCQ
1) 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; }
A) 5 RABBIT
B) 0 3 5 RABBIT 0 3 5 RABBIT
C) 0 3 5 RABBIT RABBIT
D) 3 5 RABBIT RABBIT
Answer [=] D
Explanation:
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.
2) What is the output of C Program with switch statement.?
int main() { int a=3; switch(a) { case 2: printf("ZERO "); break; case default: printf("RABBIT "); } }
A) RABBIT
B) ZERO RABBIT
C) No output
D) Compiler error
Answer [=] D
Explanation:
Notice that it is “default” not “case default“.
3) What is the output of C Program with switch statement or block.?
int main() { int a=3; switch(a) { } printf("MySwitch"); }
A) MySwitch
B) No Output
C) Compiler Error
D) None of the above
Answer [=] A
Explanation:
Yes. It is okay to omit CASE: statements inside SWITCH construct or block.
4) What is the output of C Program with switch statement or block.?
int main() { int a; switch(a) { printf("APACHE "); } printf("HEROHONDA"); }
A) APACHE HEROHONDA
B) HEROHONDA
C) No Output
D) Compiler error
Answer [=] B
Explanation:
Notice the missing CASE or DEFAULT statements. Still compiler accepts. But without CASE statement nothing will be printed inside of SWITCH.
switch(a) { printf("APACHE "); }
5) What is the output of C program with switch statement or block.?
int main() { int a; switch(a); { printf("DEER "); } printf("LION"); }
A) LION
B) DEER LION
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Notice a semicolon at the end of switch(a);. So, printf DEER is out of SWITCH.
switch(a) { ; } { printf("DEER "); }
6) What is the output of C Program with switch statement or block.?
int main() { static int a=5; switch(a) { case 0: printf("ZERO ");break; case 5: printf("FIVE ");break; case 10: printf("DEER "); } printf("LION"); }
A) ZERO FIVE DEER LION
B) FIVE DEER LION
C) FIVE LION
D) Compiler error
Answer [=] C
Explanation:
After matching 5, FIVE will be printed. BREAK causes control to exit SWITCH immediately. Also, using a STATIC variable is also allowed.
7) What is the output of C program with switch statement or block.?
int main() { char code='K'; switch(code) { case 'A': printf("ANT ");break; case 'K': printf("KING "); break; default: printf("NOKING"); } printf("PALACE"); }
A) KING PALACE
B) KING NOTHING PALACE
C) ANT KING PALACE
D) Compiler error for using Non Integers as CASE constants.
Answer [=] A
Explanation:
Any character is replaced by ASCII code or number by the compiler. So it is allowed to use CHARACTER constants for CASE constants.
8) What is the output of C Program with switch statement or block.?
int main() { char code='K'; switch(code) { case "A": printf("ANT ");break; case "K": printf("KING "); break; default: printf("NOKING"); } printf("PALACE"); }
A) ANT KING PALACE
B) KING PALACE
C) PALACE
D) Compiler error
Answer [=] D
Explanation:
You can not use STRING constants with DOUBLE QUOTES as CASE constants. Only expressions leading or constants leading to Integers are allowed.
9) What is the output of C Program with switch statement or block.?
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"); }
A) ANT KING PALACE
B) KING PALACE
C) ANT PALACE
D) Compiler error for using expressions
Answer [=] C
Explanation:
ASCII value of ‘A’ is 65. So (64+1) matches A. Also expression leading to Integers can be used as CASE Constants.
10) What is the output of C Program with switch statement or block.?
int main() { char code=64; switch(code) { case 64: printf("SHIP ");break; case 8*8: printf("BOAT "); break; default: printf("PETROL"); } printf("CHILLY"); }
A) SHIP CHILLY
B) BOAT CHILLY
C) BOAT PETROL CHILLY
D) Compiler error
Answer [=] D
Explanation:
You can not use DUPLICATE SWITCH Case Constants. 64 == (8*).
11) What is the output of C Program with switch statement or block.?
int main() { int k=64; switch(k) { case k<64: printf("SHIP ");break; case k>=64: printf("BOAT "); break; default: printf("PETROL"); } printf("CHILLY"); }
A) BOAT CHILLY
B) BOAT PETROL CHILLY
C) SHIP BOAT CHILLY
D) Compiler error
Answer [=] D
Explanation:
You can not use comparison operations with CASE statements in SWITCH.
12) What is the output of C Program with switch statement or block.?
int main() { int k=8; switch(k) { case 1==8: printf("ROSE ");break; case 1 && 2: printf("JASMINE "); break; default: printf("FLOWER "); } printf("GARDEN"); }
A) ROSE GARGEN
B) JASMINE GARDEN
C) FLOWER GARDEN
D) Compiler error
Answer [=] C
Explanation:
(1==8) is false i.e 0. (1&&2) is true AND true i.e true i.e 1. So case allowed first ZERO 0 and then ONE 1. All integer constants are allowed for SWITCH Case Constants.
13) What is the output of C Program with switch statement or block.?
int main() { int k=25; switch(k) { case 24: printf("ROSE ");break; case 25: printf("JASMINE "); continue; default: printf("FLOWER "); } printf("GARDEN"); }
A) JASMINE GARDEN
B) JASMINE FLOWER GARDEN
C) FLOWER GARDEN
D) Compiler error
Answer [=] D
Explanation:
You can not use CONTINUE; statement as SWITCH is not a LOOP like for, while and do while.
14) What is the output of C Program with switch statement or block.?
int main() { switch(24.5) { case 24.5: printf("SILVER ");break; case 25.0: printf("GOLD "); break; default: printf("TIN "); } printf("COPPER"); }
A) SILVER COPPER
B) TIN COPPER
C) COPPER
D) Compiler error
Answer [=] D
Explanation:
You can not use float, double or Strings inside Switch or Switch CASE.
15) Choose a correct statement about a C Switch Construct.
A) default case is optional inside switch.
B) break; causes the control to exit the switch immediately and avoid fall down to other CASE statements.
C) You can not use duplicate CASE Constants inside a Switch construct.
D) All the above.
Answer [=] D