Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.19)

19. 

What will be the output of the program?

public class Switch2 
{
    final static short x = 2;
    public static int y = 0;
    public static void main(String [] args) 
    {
        for (int z=0; z < 4; z++) 
        {
            switch (z) 
            {
                case x: System.out.print("0 ");
                default: System.out.print("def ");
                case x-1: System.out.print("1 ");  
                            break;
                case x-2: System.out.print("2 ");
            }
        }
    }
}

[A]. 0 def 1
[B]. 2 1 0 def 1
[C]. 2 1 0 def def
[D]. 2 1 0 def 1 def 1

Answer: Option D

Explanation:

When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3, default, then x-1 are matched. The rules for default are that it will fall through from above like any other case (for instance when z == 2), and that it will match when no other cases match (for instance when z==3).


R Vigneshram said: (Nov 7, 2010)  
Whether the output will be this

2 1 0 def 1 def 1

or

2 1 0 def 1 def

Sundar said: (Nov 8, 2010)  
Option D : 2 1 0 def 1 def 1

is the correct answers. I have tested it.

Himanshu said: (Jan 9, 2014)  
Answer "2 1 0 def 1 def 1" why last digit 1 is come ?

Violet said: (Mar 7, 2014)  
Can variables declared with final change?

Nancy said: (Nov 7, 2014)  
Guys.

1>> when i = 0 then last switch case satisfies and output is 2.

2>>value of I increments, i = 1 so x-1 case satisfies and after there is a break statement so output is 2 1.

3>> value increments to 2, i = 2 so case x satisfies and value is 0 but there is no break statement after this so below cases continues to give output till case x-1 because after that there is a break statement and output is 2 1 0 def 1.

4>> value increments to 3, i = 3 so default case is executed and continues printing below cases till break arrives so output is 2, 1, 0 def 1, def 1.

And hence final output is 2 1 0 def 1 def 1.

Ihor said: (Oct 1, 2016)  
"case x: System.out.print("0 ");
default: System.out.print("def ");
case x-1: System.out.print("1 ");
break;
case x-2: System.out.print("2 ");
"

Isn't it should be like.

-from def than 1, then break ad checking that "x-2" works so 2, etc.
So like def 1 2.

Karthik Shinde said: (Jan 17, 2017)  
In the above

case x: System.out.print("0 "); ---------------> x=2 i.e case:2
default: System.out.print("def ");---------------> this prints def
case x-1: System.out.print("1 ");--------------->x-1 = 2-1 = 1 i.e case 1
break;
ase x-2: System.out.print("2 ");---------------> x-2 = 2-2 = 0 i.e case 0

z value case chosen output
0 case 0 2
1 case 1 2 1
2 case 2 2 1 0 def 1
since there is no break all the three cases are executed

i.e case 2 , default and case1.

3, default 2 1 0 def 1 def 1.
Since default case is taken therefore case1 is taken.


The solution is 2 1 0 def 1 def 1.

Tim said: (Jan 23, 2017)  
How come the default case isn't used on the first iteration when case x does not satisfy the condition and default is the next one executed?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.