Compilers Multiple Choice Questions & Answers (MCQs) on “Switch Case”.
1. How many times will the following loop be executed?
ch = 'b'; while(ch >= 'a' && ch <= 'z')
a) 0
b) 25
c) 26
d) 1
Answer: b
Clarification: Because there are 26 alphabets and so it runs for 26-1 times.
2. Consider the following program.
switch(input) { case '1': printf("One"); case '2': printf("Two"); case '3': printf(""Three"); default: Printf("Default"); break; }
What will be printed when input is 2?
a) Two Three default
b) Two
c) Two default
d) Two Two default
Answer: a
Clarification: Because in between cases there is no break statement.
3. The advantage of ‘switch’ statement over ‘if’ is that it leads to more structured program.
a) True
b) False
Answer: a
Clarification: Switch is more convenient to use as it avoids confusion in case of more nested structure.
4. Consider the following C program. What is the Value of a?
#include int main() { int a=7, b=5; switch(a = a % b) { case 1: a = a - b; case 2: a = a + b; case 3: a = a * b; case 4: a = a / b; default: a = a; } return 0; }
a) 7
b) 5
c) 9
d) None of the mentioned
Answer: a
Clarification: Initially a=a%b gives 2. Then in case 2 a=a+b gives 2+5=7. Hence a=7.
5. The statement for(;;) is perfectly valid C statement.
a) True
b) False
Answer: b
Clarification: Infinite loop.
6. What will be the output of the following Code?
void main() { static a,b; while(a > b++) }
a) a=0 b=0
b) a=0 b=0
c) a=1 b=1
d) none of the mentioned
Answer: a
Clarification: Static variables have an initial value 0.
7. Number of times the loop will run.
for(digit = 0;digit < 9; digit++) { digit = digit *2; digit--; }
a) Infinite
b) 9
c) 0
d) 4
Answer: a
Clarification: Infinite times it runs.
8. An if statement may contain compound statements only in the else clause.
a) True
b) False
Answer: b
Clarification: If can also have compound statements.
9. A ‘while’ loop may always be converted to an equivalent ‘for’ loop.
a) True
b) False
Answer: a
Clarification: Can be converted to for loop.
10. Default’ case is mandatory in a switch statement.
a) True
b) False
Answer: b
Clarification: Default cases may or may not be written.
