+ All Categories
Home > Engineering > Switch statement mcq

Switch statement mcq

Date post: 15-Apr-2017
Category:
Upload: parthipan-parthi
View: 24 times
Download: 0 times
Share this document with a friend
21
* SWITCH Statement- MCQ
Transcript
Page 1: Switch statement  mcq

*SWITCH Statement- MCQ

Page 2: Switch statement  mcq

1. What is the output?main(){

int i=3;switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; }

}

Page 3: Switch statement  mcq

Answer :three

Explanation :The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

Page 4: Switch statement  mcq

2.#include<stdio.h>main(){int i=1,j=2;switch(i) { case 1: printf("GOOD");

break; case j: printf("BAD"); break; }}

Page 5: Switch statement  mcq

Answer:Compiler Error: Constant expression required in function main.

Explanation:The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).

Page 6: Switch statement  mcq

3.main(){

float i=1.5;switch(i)

{case 1: printf("1");

case 2: printf("2");default : printf("0");

}}

Page 7: Switch statement  mcq

Answer:Compiler Error: switch expression not integral

Explanation:Switch statements can be applied only to

integral types.

Page 8: Switch statement  mcq

4. #include<stdio.h>int main(){ int i = 1; switch(i) { case 1: printf("Case1"); break; case 1*2+4: printf("Case2"); break; }return 0;}

Page 9: Switch statement  mcq

A. Error: in case 1*2+4 statementB. Error: No default specifiedC. Error: in switch statementD. No Error

Answer: Option DExplanation:Constant expression are accepted in switchIt prints "Case1"

Page 10: Switch statement  mcq

5. #include<stdio.h>int main(){ int i=1; for(;;) { printf("%d\n", i++); if(i>10) break; } return 0;}

Page 11: Switch statement  mcq

A. There should be a condition in the for loopB. The two semicolons should be droppedC. The for loop should be replaced with while loop.D. No error

Answer: Option D

Page 12: Switch statement  mcq

6. #include<stdio.h>int main(){ int a = 10; switch(a) { } printf("This is c program."); return 0;}

Page 13: Switch statement  mcq

A. Error: No case statement specifiedB. Error: No default specifiedC. No ErrorD. Error: infinite loop occurs

Answer: Option C

Page 14: Switch statement  mcq

7. #include<stdio.h>int main(){ int i = 1; switch(i) { printf("This is c program."); case 1: printf("Case1"); break; case 2: printf("Case2"); break; }return 0;}

Page 15: Switch statement  mcq

A. Error: No default specifiedB. Error: Invalid printf statement after switch statementC. No Error and prints "Case1"D. None of above

Answer: Option C

Page 16: Switch statement  mcq

8. #include<stdio.h>int main(){ int a = 5; switch(a) { case 1: printf("First");

case 2: printf("Second");

case 3 + 2: printf("Third");

case 5: printf("Final"); break;

} return 0;}

Page 17: Switch statement  mcq

A. There is no break statement in each case.B. Expression as in case 3 + 2 is not allowed.C. Duplicate case case 5:D. No error will be reported.

Answer: Option C

Page 18: Switch statement  mcq

9. include <stdio.h> int main() { int i=0; for(i=0; i<20; i++) { switch(i) { case 0: i+=5; case 1: i+=2; case 5: i+=5;}

default: i+=4; break; } printf("%d ", i); } getchar(); return 0;

OUTPUT: 21

Page 19: Switch statement  mcq

10. #include<stdio.h>int main(){ int i=3; switch(i) { case 1: printf("Hello\n"); case 2: printf("Hi\n"); case 3: continue;

default: printf("Bye\n"); } return 0;}

OUTPUT: Misplaced Continue

Page 20: Switch statement  mcq

11. #include<stdio.h>int main(){ int i=4; switch(i) { default: printf("This is default\n"); case 1: printf("This is case 1\n"); break; case 2: printf("This is case 2\n"); break;

case 3: printf("This is case 3\n"); } return 0;}

Output:This is defaultThis is case1

Page 21: Switch statement  mcq

12. main(){float me = 1.1;double you = 1.1;if(me==you)

printf("I love SWEETS");else

printf("I hate MEDICINES");}Answer: I hate MEDICINES


Recommended