Computer Science, asked by chaudharymunna251, 4 months ago

20. What will be the output of following code fragment when the input is
(a) 'A' (b) 'C'
(c) 'D'
(d) 'F' ?
switch (ch)
case 'A': System.out.println ("Grade A");
case 'B' : System.out.println ("Grade B");
case 'C' : System.out.println ("Grade C");
break;
case 'D': System.out.println (“Grade D");
default :System.out.println (“Grade F");​

Answers

Answered by akshatgupta099
9

Answer:

(a) Grade A

Grade B

Grade C

(b) Grade C

(c) Grade D

Grade F

(d) Grade F

Explanation:

(a) After the case 'A' is executed there is no break statement so the control goes to the next step and executes it. This is called fall through. But after exiting from case 'C' there is a break statement so the control exits the switch case.

(b) There is a break statement after case 'C' so there is no fall through.

(c) After case 'D' is executed the control goes to the default case and executes it as well since there is no break statement in between.

(d) 'F' matches no case so the default block is executed.

Similar questions