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" ) ; }
Choose one option
Official answer and solution
Official answer
Marks to all
From
Official GATE 2012 answer key
Why the marks went to every candidate

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" ) ; }
  1. The variable inChar is initialized to 'A'.
  2. The switch statement matches case 'A'.
  3. The statement printf("Choice A\ n"); executes. Notice there are no break statements anywhere in the switch block, so execution falls through all subsequent cases.
  4. It falls through case 'B', reaches case 'C', and executes printf("Choice B");.
  5. It falls through case 'D', case 'E', and reaches default, executing printf(" 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.

Final answer

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.