C Programming Questions and Answers – Switch Statements – 1

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Switch Statements – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

advertisement
  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
View Answer

Answer: b
Explanation: None.
advertisement

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
View Answer

Answer: c
Explanation: None.
advertisement

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
View Answer

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

  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
View Answer

Answer: b
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, b = 1;
  5.         switch (a)
  6.         {
  7.            case a*b:
  8.               printf("yes ");
  9.            case a-b:
  10.               printf("no\n");
  11.               break;
  12.         }
  13.     }

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

Answer: c
Explanation: None.

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
View Answer

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f = 1;
  5.         switch (f)
  6.         {
  7.            case 1.0:
  8.               printf("yes\n");
  9.               break;
  10.            default:
  11.               printf("default\n");
  12.         }
  13.     }

a) yes
b) yes default
c) Undefined behaviour
d) Compile time error
View Answer

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