This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Switch Statement”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
#include <stdio.h>
void main()
{
double ch;
printf("enter a value between 1 to 2:");
scanf("%lf", &ch);
switch (ch)
{
case 1:
printf("1");
break;
case 2:
printf("2");
break;
}
}
a) Compile time error
b) 1
c) 2
d) Varies
View Answer
Explanation: None.
2. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
#include <stdio.h>
void main()
{
char *ch;
printf("enter a value between 1 to 3:");
scanf("%s", ch);
switch (ch)
{
case "1":
printf("1");
break;
case "2":
printf("2");
break;
}
}
a) 1
b) Compile time error
c) 2
d) Run time error
View Answer
Explanation: None.
3. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
#include <stdio.h>
void main()
{
int ch;
printf("enter a value between 1 to 2:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("1\n");
default:
printf("2\n");
}
}
a) 1
b) 2
c) 1 2
d) Run time error
View Answer
Explanation: None.
4. What will be the output of the following C code? (Assuming that we have entered the value 2 in the standard input)
#include <stdio.h>
void main()
{
int ch;
printf("enter a value between 1 to 2:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("1\n");
break;
printf("hi");
default:
printf("2\n");
}
}
a) 1
b) hi 2
c) Run time error
d) 2
View Answer
Explanation: None.
5. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
#include <stdio.h>
void main()
{
int ch;
printf("enter a value between 1 to 2:");
scanf("%d", &ch);
switch (ch, ch + 1)
{
case 1:
printf("1\n");
break;
case 2:
printf("2");
break;
}
}
a) 1
b) 2
c) 3
d) Run time error
View Answer
Explanation: None.
6. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a = 1, b = 1;
switch (a)
{
case a*b:
printf("yes ");
case a-b:
printf("no\n");
break;
}
}
a) yes
b) no
c) Compile time error
d) yes no
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 97;
switch (x)
{
case 'a':
printf("yes ");
break;
case 97:
printf("no\n");
break;
}
}
a) yes
b) yes no
c) Duplicate case value error
d) Character case value error
View Answer
Explanation: None.
8. What will be the output of the following C code?
#include <stdio.h>
int main()
{
float f = 1;
switch (f)
{
case 1.0:
printf("yes\n");
break;
default:
printf("default\n");
}
}
a) yes
b) yes default
c) Undefined behaviour
d) Compile time error
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.
- Practice Computer Science MCQs
- Check C Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books