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.

advertisement

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: None.
advertisement
advertisement

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

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

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

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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.