Question 1 |
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 | |
Character is not allowed in switch case as "Case Label" | |
Uppercase and Lowercase Characters cannot be used together. |
Question 1 Explanation:
case 'A' and case 'a' Share same set of statements because of single break statement.
Question 2 |
Switch Case Label should be Unique
State Whether True/False ?True | |
False |
Question 2 Explanation:
Switch Case Label Should be Unique.
Question 3 |
Check whether following code will generate any output or not ?
void main() { int choice = 10 ; switch(choice) { case <10 : printf("\nLess than 10"); break; case =10 : printf("\nEqual to 10"); break; case >10 : printf("\nGreater than 10"); break; } }
No | |
Yes |
Question 3 Explanation:
- Case Labels won't allow you to use Comparison Operators
Question 4 |
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 4 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.
Question 5 |
Check whether code will work properly or not -
void main() { float choice = 10.1 ; switch(choice) { case 10.1 : printf("\nCase 10.1"); break; case 10.2 : printf("\nCase 10.2"); break; case 10.3 : printf("\nCase 10.3"); break; } }
No | |
Yes |
Question 5 Explanation:
- Case Labels should not contain Floating Number
Question 6 |
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 2 | |
No Output | |
This is Paramete 3 | |
Compile Error | |
This is Paramete 1 |
Question 6 Explanation:
- Comma Returns Rightmost Operand
- Rightmost variable is 'choice+2' so it returns 'choice+2'
- Thus Case 3 will be executed
Question 7 |
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; } }
This is Roll No - 2 | |
Compile Time Error | |
This is Roll No - 1 | |
Run Time Error | |
This is Roll No - 3 |
Question 7 Explanation:
- Variable declared with const is allowed in the Switch Case.
- Variable declared with const will have permanent value after the first initialization.
Question 8 |
Check whether code will work properly or not -
void main() { int choice = 1 ; switch(choice) { case 1 : printf("\nCase Integer 1"); break; case '1' : printf("\nCase Character 1"); break; } }
Yes | |
No |
Question 8 Explanation:
- Case Label Should be Integer / Constant in case of character.
- Characters are Converted into Integer internally using ASCII
case 1=case1 case'1'=case(ASCII Value of Digit 1) =case49
Question 9 |
State whether following code is error free or not -
void main() { int choice = -2 ; switch(choice) { case -1 : printf("\nCase -1"); break; case -2 : printf("\nCase -2"); break; } }
No | |
Yes |
Question 9 Explanation:
Switch case can have negative case labels.
Question 10 |
State whether following code will execute or not ?
void main() { int choice = 2 ; switch(choice) { case 1+2/3 : printf("\nCase 1"); break; case 2/2*3 : printf("\nCase 3"); break; } }
Yes | |
No |
Question 10 Explanation:
- Case Label Should be Integer / Constant
- Constant Expression in Case Labels Must Results in Constant Value
case 1+2/3=case(1+0) =case1 case2/2*3=case(1*3) =case3
There are 10 questions to complete.