C Programming Questions and Answers – Switch Statements – 2

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?

  1.     #include <stdio.h>
  2.     const int a = 1,  b = 2;
  3.     int main()
  4.     {
  5.         int x = 1;
  6.         switch (x)
  7.         {
  8.            case a:
  9.               printf("yes ");
  10.            case b:
  11.               printf("no\n");
  12.               break;
  13.         }
  14.     }

a) yes no
b) yes
c) no
d) Compile time error
View Answer

Answer: d
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:

advertisement
advertisement
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?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <stdio.h>
  2.     #define max(a) a
  3.     int main()
  4.     {
  5.         int x = 1;
  6.         switch (x)
  7.         {
  8.            case max(2):
  9.               printf("yes\n");
  10.            case max(1):
  11.               printf("no\n");
  12.               break;
  13.         }
  14.     }

a) yes no
b) yes
c) no
d) Compile time error
View Answer

Answer: c
Explanation: None.
advertisement

3. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         switch (printf("Do"))
  5.         {
  6.            case 1:
  7.               printf("First\n");
  8.               break;
  9.            case 2:
  10.               printf("Second\n");
  11.               break;
  12.            default:
  13.               printf("Default\n");
  14.               break;
  15.         }
  16.     }

a) Do
b) DoFirst
c) DoSecond
d) DoDefault
View Answer

Answer: c
Explanation: None.

4. Comment on the output of the following C code.

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1;
  5.         switch (a)
  6.         case 1:
  7.             printf("%d", a);
  8.         case 2:
  9.             printf("%d", a);
  10.         case 3:
  11.             printf("%d", a);
  12.         default:
  13.             printf("%d", a);
  14.     }

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

Answer: d
Explanation: None.

5. Which datatype can accept the switch statement?
a) int
b) char
c) long
d) all of the mentioned
View Answer

Answer: d
Explanation: None.

6. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1;
  5.         switch (a)
  6.         {
  7.            case a:
  8.               printf("Case A ");
  9.            default:
  10.               printf("Default");
  11.         }
  12.     }

a) Output: Case A
b) Output: Default
c) Output: Case A Default
d) Compile time error
View Answer

Answer: d
Explanation: None.

7. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     switch (ch)
  3.     {
  4.        case 'a':
  5.        case 'A':
  6.           printf("true");
  7.     }

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

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.