Switch MCQ Ans
Switch MCQ Ans
1. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard
input)
1. #include <stdio.h>
2. void main()
3. {
4. double ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%lf", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) Compile time error
b) 1
c) 2
d) Varies
Answer: a
2. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard
input)
1. #include <stdio.h>
2. void main()
3. {
4. char *ch;
5. printf("enter a value between 1 to 3:");
6. scanf("%s", ch);
7. switch (ch)
8. {
9. case "1":
10. printf("1");
11. break;
12. case "2":
13. printf("2");
14. break;
15. }
16. }
a) 1
b) Compile time error
c) 2
d) Run time error
Answer: b
3. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard
input)
1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. default:
12. printf("2\n");
13. }
14. }
a) 1
b) 2
c) 1 2
d) Run time error
Answer: c
4. What will be the output of the following C code? (Assuming that we have entered the value 2 in the standard
input)
1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. printf("hi");
13. default:
14. printf("2\n");
15. }
16. }
a) 1
b) hi 2
c) Run time error
d) 2
Answer: d
5. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard
input)
1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch, ch + 1)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) 1
b) 2
c) 3
d) Run time error
Answer: b
Answer: c
7. What will be the output of the following C code?
1. #include <stdio.h>
2. int main()
3. {
4. int x = 97;
5. switch (x)
6. {
7. case 'a':
8. printf("yes ");
9. break;
10. case 97:
11. printf("no\n");
12. break;
13. }
14. }
a) yes
b) yes no
c) Duplicate case value error
d) Character case value error
Answer: c
Answer: d
Answer: d
Answer: c
Answer: c
Answer: d
5. Which datatype can accept the switch statement?
a) int
b) char
c) long
d) all of the mentioned
Answer: d
Answer: d