# Java Programming - Flow Control - Discussion

Discussion Forum : 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 ");
}
}
}
}
```

Answer: Option

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*).

Discussion:

8 comments Page 1 of 1.
Tim said:
6 years ago

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?

Karthik shinde said:
6 years ago

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.

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.

Ihor said:
7 years ago

"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.

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.

Nancy said:
9 years ago

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.

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.

Violet said:
9 years ago

Can variables declared with final change?

Himanshu said:
9 years ago

Answer "2 1 0 def 1 def 1" why last digit 1 is come ?

Sundar said:
1 decade ago

Option D : 2 1 0 def 1 def 1

is the correct answers. I have tested it.

is the correct answers. I have tested it.

R Vigneshram said:
1 decade ago

Whether the output will be this

2 1 0 def 1 def 1

or

2 1 0 def 1 def

2 1 0 def 1 def 1

or

2 1 0 def 1 def

Post your comments here:

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers