Compilers Questions and Answers – Switch Case – 2

This set of Compilers Multiple Choice Questions & Answers (MCQs) focuses on “Switch Case – 2”.

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
View Answer

Answer: b
Explanation: Because there are 26 alphabets and so it runs for 26-1 times.
advertisement
advertisement

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: Switch is more convenient to use as it avoids confusion in case of more nested structure.
advertisement

4. Consider the following C program. What is the Value of a?

#include<stdio.h>
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
View Answer

Answer: a
Explanation: Initially a=a%b gives 2. Then in case 2 a=a+b gives 2+5=7. Hence a=7.
advertisement

5. The statement for(;;) is perfectly valid C statement.
a) True
b) False
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: Static variables have an initial value 0.

7. Number of times the loop will run.

for(digit = 0;digit &lt; 9; digit++)
{
    digit = digit *2;
    digit--;
}

a) Infinite
b) 9
c) 0
d) 4
View Answer

Answer: a
Explanation: Infinite times it runs.

8. An if statement may contain compound statements only in the else clause.
a) True
b) False
View Answer

Answer: b
Explanation: If can also have compound statements.

9. A ‘while’ loop may always be converted to an equivalent ‘for’ loop.
a) True
b) False
View Answer

Answer: a
Explanation: Can be converted to for loop.

10. Default’ case is mandatory in a switch statement.
a) True
b) False
View Answer

Answer: b
Explanation: Default cases may or may not be written.

Sanfoundry Global Education & Learning Series – Compilers.

To practice all areas of Compilers, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.