What will be the output of the following C program segment?
char inChar = 'A';
switch ( inChar ) {
case 'A' : printf ("Choice A \ n");
case 'B' :
case 'C' : printf ("Choice B");
case 'D' :
case 'E' :
default : printf ("No Choice");
}Marks were awarded to all candidates for this question. It is not scored as a normal quiz.
View worked solution
Marks to all
This question was awarded Marks to All (MTA) by GATE due to typographical errors in the options and code. Below is the detailed step-by-step execution and explanation of the behavior.
1. Concept of Switch-Case Fall-Through
In C, the switch statement executes the matching case label. If a case block does not end with a break statement, the execution continues ("falls through") into the subsequent cases and the default block, regardless of whether those case labels match the switch expression.
2. Step-by-Step Execution
- Initialization:
char inChar = 'A'; - The
switch (inChar)matchescase 'A'. - It executes:
printf ("Choice A \ n");
(Assuming\ nis a typo for the newline character\n, this printsChoice Afollowed by a newline). - Since there is no
breakstatement at the end ofcase 'A', the control falls through tocase 'B'and thencase 'C'. - It executes:
printf ("Choice B"); - Again, there is no
breakstatement, so control falls throughcase 'D',case 'E', and finally thedefaultblock. - It executes:
printf ("No Choice");
3. Expected Output vs Options
Combining the outputs sequentially, the exact output of the program is:
Choice A Choice BNo Choice
Why Marks to All (MTA) was given:
- There is no space printed between
Choice BandNo Choicein the actual execution, but Option C includes a space:Choice B No Choice. - The code contains a space inside the escape sequence (
\ ninstead of\n), which is syntactically a literal backslash and space in many contexts rather than a newline.
Because of these discrepancies, none of the options are technically correct, and full marks were awarded to all candidates.
Free practice · Your answers on this page are not saved.
