C Programming Questions and Answers on Switch Statements for Freshers

https://www.computersprofessor.com/2017/11/c-programming-questions-and-answers-on_27.html
1. What is the output of this C code(when 1 is entered)?
#include
void main()
{
double ch;
printf("enter a value btw 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
b) 1
c) 2
d) Varies
Answer: a
2. What is the output of this C code(When 1 is entered)?
#include
void main()
{
char *ch;
printf("enter a value btw 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
b) Compile time error
c) 2
d) Run time error
Answer: b
3. What is the output of this C code(When 1 is entered)?
#include
void main()
{
int ch;
printf("enter a value btw 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
b) 2
c) 1 2
d) Run time error
Answer: c
4. What is the output of this C code(When 2 is entered)?
#include
void main()
{
int ch;
printf("enter a value btw 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
b) hi 2
c) Run time error
d) 2
Answer: d
5. What is the output of this C code(When 1 is entered)?
#include
void main()
{
int ch;
printf("enter a value btw 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
b) 2
c) 3
d) Run time error
Answer: b
6. What is the output of this C code?
#include
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
b) no
c) Compile time error
d) yes no
Answer: c
7. What is the output of this C code?
#include
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
b) yes no
c) Duplicate case value error
d) Character case value error
Answer: c
8. What is the output of this C code?
#include
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
b) yes default
c) Undefined behaviour
d) Compile time error
Answer: d
9. What is the output of this C code?
#include
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
b) yes
c) no
d) Compile time error
Answer: d
10. What is the output of this C code?
#include
#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
b) yes
c) no
d) Compile time error
Answer: c
11. What is the output of this C code?
#include
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
b) DoFirst
c) DoSecond
d) DoDefault
Answer: c
12. Comment on the output of this C code?
#include
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
b) No error, output is 1
c) Compile time error, no break statements
d) Compile time error, case label outside switch statement
Answer: d
13. Switch statement accepts.
a) int
b) char
c) long
d) all of the mentioned
Answer: d
14. Comment on the output of this C code?
#include
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
b) Output: Default
c) Output: Case A Default
d) Compile time error
Answer: d
15. Comment on the output of this C code?
#include
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
b) if (ch == ‘a’)
if (ch == ‘a’) printf(“true”);
c) if (ch == ‘a’ || ch == ‘A’) printf(“true”);
d) none of the mentioned
Answer: c