C Programming Questions and Answers on Switch Statements for Freshers

1. What is the output of this C code(when 1 is entered)?

  1.     #include 
  2.     void main()
  3.     {
  4.         double ch;
  5.         printf("enter a value btw 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 is the output of this C code(When 1 is entered)?
  1.     #include 
  2.     void main()
  3.     {
  4.         char *ch;
  5.         printf("enter a value btw 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 is the output of this C code(When 1 is entered)?
  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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 is the output of this C code(When 2 is entered)?

  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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 is the output of this C code(When 1 is entered)?
  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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
6. What is the output of this C code?
  1.     #include 
  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
Answer: c
7. What is the output of this C code?

  1.     #include 
  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

8. What is the output of this C code?

  1.     #include 
  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
Answer: d
9. What is the output of this C code?
  1.     #include 
  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
Answer: d
10. What is the output of this C code?
  1.     #include 
  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
Answer: c
11. What is the output of this C code?
  1.     #include 
  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
Answer: c
12. Comment on the output of this C code?
  1.     #include 
  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
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?
  1.     #include 
  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
Answer: d
15. Comment on the output of this C code?
  1.     #include 
  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
Answer: c

Related

Multiple choice Questions and Answers on Resource Ceilings of Cloud Computing for Freshers

1. Which of the following resource(s) represents the bottleneck in the current system that limits the system’s performance ? a) ROMb) Resource ceilingc) Resource Parametersd) All of the mentioned ...

Multiple choice Questions and Answers on Baseline Measurements of Cloud Computing for Freshers

1. Which of the following is another name for resources mentioned in the following figure ?a) System Metricsb) System Baselinec) System Parametersd) All of the mentioned Answer: a Explanation: A m...

Multiple choice Questions and Answers on Capacity Planning of Cloud Computing for Freshers

1. Which of the following correctly describes the following figure ?a) Capacity Planningb) Performance Planningc) Network Planningd) None of the mentioned Answer: a Explanation: Capacity planning ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item