The GATE Grind

GATE 2012 CS (CS1) – Question 13

Programming and Data Structures · Programming in C · 1 mark · MTA

Find the output of the printed C fragment. Several case labels below retain the source’s semicolons.

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. No Choice
  2. Choice A
  3. Choice A, then Choice B No Choice
  4. Program gives no output as it is erroneous

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: MTA

Explanation

The final key gives marks to all. Literally, the printed semicolons after case B,D,E are invalid case-label syntax. If repaired to colons, lack of break causes fall-through from A through the later print statements, producing Choice A followed by Choice B and No Choice. Preserve universal credit and disclose the source defect.

More Programming in C questions

All Programming in C questions →