Menu
Topics Index
...
`

SwitchInSwitch


If you need explanation Read this topic

If you need Answer Take test on this topic

SwitchInSwitch
What will be the output of the following program?
public class SwitchInSwitch {
    public static void main(String[] args) {
        int x = 3, y = 4, z = 5;
        switch (x) {
            case 5 :
                System.out.print(z + "~");
            case 3 :
                System.out.print(x + "~");
                switch (y) {
                    case 3 :
                        System.out.print(x + "~");
                    case 4 :
                        System.out.print(y + "~");
                        switch (z) {
                            case 5 :
                                System.out.print(z + "~");
                            case 4 :
                                System.out.print(y + "~");
                        }
                }
        }
    }
}

A. 3~4~5~
B. 3~4~5~4~
C. 5~3~3~4~5~4~
D. Compilation Error
E. Runtime Error
Topic: Nested switch Statements In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Option-B because it prints 3 in case x  case y : 4, case z 5 , 4. Since there is no break.

Posted by Sivani Reddy Kakuru    2014-11-27 04:31:05


a..frstlt swtch(x) as x= 3 so case 5 will not wrk n case 3 will wrk n no brk is thre it wil prnt 3  ...... den switch(y) -it will wwrk fr case 4 since y=4 ....den fr switch(z) it wil prnt z=3

Posted by Asma Mujtaba Khan    2014-11-27 04:33:14


b

Posted by Abhishek Kumar    2014-11-27 04:34:12


Answer is B....in the first switch statement control goes to case 3 where 3~ is printed and then in the next switch y=4,control goes to case 4 and 4~ is printed and then in the next switch z=5,control goes to case 5 and prints 5~ and since there is no break,control  does not come out of switch and it goes to case 4 and prints 4~

Posted by Shashanka Mogaliraju    2014-11-27 04:35:37


Ans is A switch statement makes a selected execution based on some expression
break statement terminates from the switch block or nested switch block
the control enters to x=3  then it prints x=3 and ~
similarly the control enters y=4,it selectes the statements prints 4 and ~
the control enters z=5 nad prints z and ~
if break statement would have been there after first switch block,then only 3 ~ is printed,,,,

Posted by Sai Veerendra    2014-11-27 04:38:34


b

Posted by Pavani Blossom    2014-11-27 04:51:44


ans b. It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.  Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types    the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer  The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.  
Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.
Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.                               coming to this program x,y,z int variables initialized with 3,4,5 respectively.                              outer switch case have x .so if it is found matched case thn that case get executed.here case 3 is present so 3~ get printed.nxt inner switch has y,case 4 prints 4~,thn nxt inner switch have z, here case 5 get executed and prints 5~ thn case 4 get executed 4~ bcz no break statemnt encountered.

Posted by Maheshwari Natarajan    2014-11-27 18:01:13


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Sai Ram, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-28 01:41:13


© meritcampus 2019

All Rights Reserved.

Open In App