This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Switch Statements – 2”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?
#include <stdio.h>
const int a = 1, b = 2;
int main()
{
int x = 1;
switch (x)
{
case a:
printf("yes ");
case b:
printf("no\n");
break;
}
}
a) yes no
b) yes
c) no
d) Compile time error
View Answer
Explanation: We are violating a C programming rule which states that in switch-case statements, the labels must be integer constants. When you compile the code, the c compiler will give an error message indicating that the case label is not an integer constant because the labels given in the code are integer variables ‘a’ and ‘b’. You will get the following error message:
error: case label does not reduce to an integer constant case a: error: case label does not reduce to an integer constant case b:
2. What will be the output of the following C code?
#include <stdio.h>
#define max(a) a
int main()
{
int x = 1;
switch (x)
{
case max(2):
printf("yes\n");
case max(1):
printf("no\n");
break;
}
}
a) yes no
b) yes
c) no
d) Compile time error
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
switch (printf("Do"))
{
case 1:
printf("First\n");
break;
case 2:
printf("Second\n");
break;
default:
printf("Default\n");
break;
}
}
a) Do
b) DoFirst
c) DoSecond
d) DoDefault
View Answer
Explanation: None.
4. Comment on the output of the following C code.
#include <stdio.h>
int main()
{
int a = 1;
switch (a)
case 1:
printf("%d", a);
case 2:
printf("%d", a);
case 3:
printf("%d", a);
default:
printf("%d", a);
}
a) No error, output is 1111
b) No error, output is 1
c) Compile time error, no break statements
d) Compile time error, case label outside switch statement
View Answer
Explanation: None.
5. Which datatype can accept the switch statement?
a) int
b) char
c) long
d) all of the mentioned
View Answer
Explanation: None.
6. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a = 1;
switch (a)
{
case a:
printf("Case A ");
default:
printf("Default");
}
}
a) Output: Case A
b) Output: Default
c) Output: Case A Default
d) Compile time error
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
switch (ch)
{
case 'a':
case 'A':
printf("true");
}
a) if (ch == ‘a’ && ch == ‘A’) printf(“true”);
b)
if (ch == 'a') if (ch == 'a') printf("true");
c) if (ch == ‘a’ || ch == ‘A’) printf(“true”);
d) none of the mentioned
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship