C Programming MCQ : Switch Case Multiple Choice Questions
Question 11 |
Guess the Output of the Following Program -
#include<stdio.h> #define var 1 void main() { int choice = 1 ; switch(choice) { case var: printf("\I am in Case 1"); break; case 2: printf("\nI am in Case 2"); break; case 3: printf("\nI am in Case 3"); break; } }
Run Time Error | |
I am in Case 1 | |
I am in Case 3 | |
I am in Case 2 | |
Compile Error |
Question 11 Explanation:
- #define is called as Preprocessor
- Before Executing Program Macro processor just replaces the 'var' by its eqivalent definition
- Here 'var' is replaced by 1 and case 1 will be executed
Question 12 |
Guess the Output of the Following Program -
#include<stdio.h> void main() { int choice = 1 ; int const var = 2; switch(choice) { case 1: printf("\This is Roll No - 1"); break; case var: printf("\This is Roll No - 2"); break; case 3: printf("\This is Roll No - 3"); break; } }
Run Time Error | |
This is Roll No - 1 | |
This is Roll No - 3 | |
Compile Time Error | |
This is Roll No - 2 |
Question 12 Explanation:
- Variable declared with const is allowed in the Switch Case.
- Variable declared with const will have permanent value after the first initialization.
Question 13 |
Guess the Output of the Following Program -
void main() { int choice = 1 ; switch(choice,choice+1,choice+2) { case var: printf("\nThis is Paramete 1"); break; case 2: printf("\nThis is Paramete 2"); break; case 3: printf("\nThis is Paramete 3"); break; } }
This is Paramete 3 | |
Compile Error | |
No Output | |
This is Paramete 2 | |
This is Paramete 1 |
Question 13 Explanation:
- Comma Returns Rightmost Operand
- Rightmost variable is 'choice+2' so it returns 'choice+2'
- Thus Case 3 will be executed
Question 14 |
Point out the Error in the following Code -
void main() { int choice = 1 ; switch(choice) { case 'A' : case 'a' : printf("\nA for Apple"); break; case 'B' : case 'b' : printf("\nB for Ball"); break; } }
Switch Case Does not contain any blank case. | |
No Error | |
Uppercase and Lowercase Characters cannot be used together. | |
Character is not allowed in switch case as "Case Label" |
Question 14 Explanation:
case 'A' and case 'a' Share same set of statements because of single break statement.
Question 15 |
Is program is error free ? - (Character Variable is passed to switch)
void main() { char choice = 'a' ; switch(choice) { case 'A' : case 'a' : printf("\nA for Apple"); break; case 'B' : case 'b' : printf("\nB for Ball"); break; } }
Yes | |
No |
Question 15 Explanation:
- Switch case can accept Integer are Characters as case Label and as parameter.
- Character Type in C can be converted into Integer called ASCII value.