• retagged by
20,814 views
66 66 votes

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"); 
    }
  1. No Choice
  2. Choice A
  3. Choice A
    Choice B No Choice
  4. Program gives no output as it is erroneous

6 Answers

Best answer
67 67 votes

There is a `space` in between the `\` and `n` in the printf of case ‘A’ and also no space after the printf for case ‘C’ in the original question (see-Q-no.-3). Due to this “Marks to All” was done.

Lets assume no space for “\n” in the printf for case ‘A’ and there is a space at the end of printf of case ‘C’.

When a case of a switch is matched, execution continues from the matched case until a “break” is encountered (any intermediate case labels simply gets ignored).

So, output of the given program is:

Choice A
Choice B No Choice 

Option C.

• edited by
27 27 votes
There is no break statement in  any case . If a case doesn’t contain break, then all the subsequent cases are executed until a break statement is found. That is why everything inside the switch is printed.

so ans is C
7 7 votes

Syntax:

  1. switch(expression)
  2. {
  3. case constant-exp1 : statements; break;
  4. case constant-exp2: statements; break;
  5. default: statements;
  6. }

In switch case, the break statement is used to terminate the switch case. Basically it is used to execute the statements of a single case statement. If no break appears, the flow of control will fall through all the subsequent cases until a break is reached or the closing curly brace ‘}’ is reached.

In above , switch case don't have break statement , 

1. If inChar='A'  , flow control will execute all statements after Case A: till switch case end (in case any break states occurs between statement , it will terminated at the point but in above example we don't have any break statement, so whole switch block will execute).

Output : Choice A
               Choice B No Choice

In case if inChar='C' then output will be  Choice B No Choice.

it's not necessary that we should always add break statement for each cases in switch, including break statement in switch case is based on our requirement.

Answer is C

• edited by
1 1 vote

In switch case if a condition contains a break that case is being executed than the switch case will terminate

 switch ( inChar ) {

       case 'A' : printf ("Choice A \ n");

WE CAN SEE HERE THAT there is no break statement inside case though in char is A
 BT THIS STATEMENT WILL NOT TERMINATE as it doesnt contain break
here output will be Choice A


so now case B is being executed

       case 'B'

here there is nothing inside so now case C will execute

       case 'C' : printf ("Choice B");
noe case C will execute and Choice B is printed
ALSO POINT TO BE NOTED AS DUE TO /n IN case A the curser had moved to nct line
so “Choice B” will be executed in next line.

now case D will be executed

       case 'D' :
it also doesnt contain any statement so  now case E will execute
which also contain no statement

       case 'E' :

so in last default will also execute

       default : printf ("No Choice");

and No Choice will be printed . 

AND THIS LEAD TO END OF SWITCH CASE AND HERE THERE WAS NO break statement so all cases got executed 

 

SO FINAL OUTPUT WILL BE

Choice A

Choice B No Choice

 

1 1 vote
C , it matches with case a then falls through till it encounters a break statement.
Answer:
Position:
Show:

Related questions

86 86 votes
7 7 answers
29.3k
29.3k views
gatecse asked Sep 15, 2014
29,256 views
Let $G$ be a weighted graph with edge weights greater than one and $G'$ be the graph constructed by squaring the weights of edges in $G$. Let $T$ and $T'$ be the minimum ...
182 182 votes
12 answers 12 answers
56.4k
56.4k views
gatecse asked Sep 12, 2014
56,365 views
Let $G$ be a complete undirected graph on $6$ vertices. If vertices of $G$ are labeled, then the number of distinct cycles of length $4$ in $G$ is equal to$15$$30$$90$$36...
126 126 votes
2 answers 2 answers
36.9k
36.9k views
go_editor asked Feb 16, 2015
36,886 views
Consider the following C program:#include<stdio.h int main() { int i, j, k = 0; j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; k-= j; for (i=0; i<5; i++) { switch(i+k) { case 1: case 2: ...