Here is a listing of C Objective Questions on “Switch Statements” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include -
const int a = 1, b = 2;
-
int main()
-
{ -
int x = 1;
-
switch (x)
-
{ -
case a:
-
printf("yes ");
-
case b:
-
printf("non");
-
break;
-
} -
}
a) yes no
b) yes
c) no
d) Compile time error
Answer: d
Clarification: None.
2. What will be the output of the following C code?
-
#include -
#define max(a) a -
int main()
-
{ -
int x = 1;
-
switch (x)
-
{ -
case max(2):
-
printf("yesn");
-
case max(1):
-
printf("non");
-
break;
-
} -
}
a) yes no
b) yes
c) no
d) Compile time error
Answer: c
Clarification: None.
3. What will be the output of the following C code?
-
#include -
int main()
-
{ -
switch (printf("Do"))
-
{ -
case 1:
-
printf("Firstn");
-
break;
-
case 2:
-
printf("Secondn");
-
break;
-
default:
-
printf("Defaultn");
-
break;
-
} -
}
a) Do
b) DoFirst
c) DoSecond
d) DoDefault
Answer: c
Clarification: None.
4. Comment on the output of the following C code.
-
#include -
int main()
-
{ -
int a = 1;
-
switch (a)
-
case 1:
-
printf("%d", a);
-
case 2:
-
printf("%d", a);
-
case 3:
-
printf("%d", a);
-
default:
-
printf("%d", a);
-
}
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
Clarification: None.
5. Which datatype can accept the switch statement?
a) int
b) char
c) long
d) all of the mentioned
Answer: d
Clarification: None.
6. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int a = 1;
-
switch (a)
-
{ -
case a:
-
printf("Case A ");
-
default:
-
printf("Default");
-
} -
}
a) Output: Case A
b) Output: Default
c) Output: Case A Default
d) Compile time error
Answer: d
Clarification: None.
7. What will be the output of the following C code?
-
#include -
switch (ch)
-
{ -
case 'a':
-
case 'A':
-
printf("true");
-
}
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
Clarification: None.
