What will be the output of the following C program segment? char inChar = 'A';…
2012
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"); }- A.
No Choice
- B.
Choice A
- C.
Choice A
Choice B No Choice
- D.
Program gives no output as it is erroneous
Attempted by 496 students.
Show answer & explanation
Correct answer: C
Key idea: a switch case in C falls through to subsequent cases if there is no break statement.
The switch compares inChar to the cases and finds a match at the case for 'A'.
It executes printf("Choice A\n"); for the matched case.
Because there is no break, execution falls through to the subsequent cases: it reaches the printf("Choice B") and executes it.
Execution continues to fall through and finally executes the default printf("No Choice").
Output (exact):
Choice A
Choice BNo Choice
Note: The first printf includes a newline, so "Choice A" appears on its own line. The "Choice B" and "No Choice" strings are printed consecutively with no intervening space, producing "Choice BNo Choice" on the next line.
A video solution is available for this question — log in and enroll to watch it.