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 &gt;= 'a' && ch &lt;= 'z')```

a) 0
b) 25
c) 26
d) 1

Explanation: 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

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

Explanation: 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<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

Explanation: 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

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

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

Explanation: Infinite times it runs.

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

Explanation: If can also have compound statements.

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

Explanation: Can be converted to for loop.

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

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.