Switch-case — C++ MCQs

20
Score: 0
Attempted: 0/20
1. What will be the output of the following code?
int x = 2;
switch(x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
case 3: cout << "Three"; break;
}




2. What will the following program print?
int num = 5;
switch(num) {
case 1: cout << "A"; break;
case 5: cout << "B"; break;
default: cout << "C";
}




3. What will be printed?
int x = 10;
switch(x) {
case 5: cout << "Five"; break;
case 10: cout << "Ten"; break;
default: cout << "None";
}




4. What happens when the code runs?
int x = 3;
switch(x) {
case 1: cout << "A";
case 2: cout << "B";
case 3: cout << "C";
}




5. What will this code output?
int val = 2;
switch(val) {
case 1: cout << "X"; break;
case 2: cout << "Y";
case 3: cout << "Z"; break;
default: cout << "W";
}




6. Identify the output:
int x = 4;
switch(x) {
case 1: cout << "A"; break;
case 2: cout << "B"; break;
default: cout << "C";
}




7. Which statement is true about the following code?
int x = 5;
switch(x) {
case 5: cout << "Hello";
case 6: cout << "World";
}




8. What will be printed?
int a = 1;
switch(a + 1) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
default: cout << "Other";
}




9. What is the output?
int n = 0;
switch(n) {
case 0: cout << "Zero"; break;
case 1: cout << "One"; break;
default: cout << "Other";
}




10. What will happen when this code executes?
char ch = 'B';
switch(ch) {
case 'A': cout << "Apple"; break;
case 'B': cout << "Banana"; break;
default: cout << "Other";
}




11. What will the following output?
int n = 2;
switch(n * 2) {
case 2: cout << "Two"; break;
case 4: cout << "Four"; break;
default: cout << "None";
}




12. What happens in this case?
int n = 1;
switch(n) {
case 1:
case 2: cout << "Match"; break;
default: cout << "No match";
}




13. Identify the error in this code:
float x = 1.5;
switch(x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
}




14. What will be printed?
int num = 7;
switch(num) {
case 5: cout << "Five"; break;
case 7: cout << "Seven"; break;
default: cout << "None";
}




15. What will this code produce?
int val = 9;
switch(val) {
case 10: cout << "Ten"; break;
case 11: cout << "Eleven";
default: cout << "Default";
}




16. What is the output of the following?
int n = 3;
switch(n) {
case 1: cout << "One";
case 2: cout << "Two";
case 3: cout << "Three";
default: cout << "Done";
}




17. What will happen here?
int x = 10;
switch(x) {
case 5: cout << "Five";
case 10: cout << "Ten"; break;
case 15: cout << "Fifteen";
}




18. What is the output?
int x = 1;
switch(x) {
case 0: cout << "Zero"; break;
case 1: cout << "One";
default: cout << "End";
}




19. What happens when this code runs?
int x = 2;
switch(x) {
case 1: cout << "Case1";
case 2: cout << "Case2";
case 3: cout << "Case3";
default: cout << "Default";
}




20. What will be printed?
int a = 4;
switch(a) {
case 2: cout << "Two"; break;
case 4: cout << "Four"; break;
case 6: cout << "Six"; break;
default: cout << "Other";
}




Contents Copyrights Reserved By T4Tutorials