Question
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" ) ; }
Official answer and solution
- Official answer
- Marks to all
- From
- Official GATE 2012 answer key
Official GATE answer key awarded Marks to All for this question.
Solution
Trace the execution of the switch-case construct in C and examine the formatting and printing behavior.
Consider the C code 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" ) ; }
- The variable
inCharis initialized to'A'. - The
switchstatement matchescase 'A'. - The statement
printf("Choice A\ n");executes. Notice there are nobreakstatements anywhere in the switch block, so execution falls through all subsequent cases. - It falls through
case 'B', reachescase 'C', and executesprintf("Choice B");. - It falls through
case 'D',case 'E', and reachesdefault, executingprintf(" No Choice");.
However, in the printed exam question, printf ("Choice A\ n") contained a space between the backslash and the 'n' (\ n), which is an invalid escape sequence in standard C (yielding compiler-dependent warnings or undefined/unspecified character output rather than a proper newline), and the printed choices in the paper had inconsistent spacing and line breaks.
Because of these typographic and specification ambiguities, GATE officially awarded Marks to All.
Marks to All
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2012 Computer Science and Information Technology (CS)
- Question
- 3 of 65, printed as CS Q.3
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.