Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - Finding the output (Q.No. 3)
3.
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 < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
Answer: Option
Explanation:
The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.
Discussion:
26 comments Page 2 of 3.
Prashant Chittam said:
1 decade ago
For those guys who don't understand the code.
Why 3rd condition met..
x is static and its 2.
So 2-2 is the first condition for switch and for loop z=0 is true which is less than 3.
So 2 is getting print.
Now z++ is increased to 1 so the value of z=1.
Again 2-1 is the second condition for switch, and for loop z=1 is true which is less than 3.
So 1 is getting print...with 2 because after printing 1 ..there is no break so that it continue with print 2.
Now the last ...z++ is increased to 2 so the value of z=2.
Which is our static final variable which satisfies case 1.
So it prints 0 with 1 and 2. Because again there is no break so that is continues printing up to end of switch block.
Read carefully.
Why 3rd condition met..
x is static and its 2.
So 2-2 is the first condition for switch and for loop z=0 is true which is less than 3.
So 2 is getting print.
Now z++ is increased to 1 so the value of z=1.
Again 2-1 is the second condition for switch, and for loop z=1 is true which is less than 3.
So 1 is getting print...with 2 because after printing 1 ..there is no break so that it continue with print 2.
Now the last ...z++ is increased to 2 so the value of z=2.
Which is our static final variable which satisfies case 1.
So it prints 0 with 1 and 2. Because again there is no break so that is continues printing up to end of switch block.
Read carefully.
Ali said:
1 decade ago
Raju you explained very well and easily.
Prasanth x is static but in this time final is important any way you also explained well.
Thanks all of you.
Prasanth x is static but in this time final is important any way you also explained well.
Thanks all of you.
Anshu said:
1 decade ago
Why we check the conditions from last but not from first?
Hardik patel said:
1 decade ago
But you have passed z as an argument in switch case, and in the case you are doing simple function irrespective of z.
I don't understand it what's the work of z in x, x-1, x-2.
I don't understand it what's the work of z in x, x-1, x-2.
Sekhar said:
1 decade ago
Friends am really not getting the point could please explain any one I need a answer regarding this question?
Vijay yadav said:
1 decade ago
Friends x is declared as final so value of x never change please give me correct answer. I haven't satisfy by anyone answer.
Rishanth said:
10 years ago
If we do not have a break instruction, the next instructions in the loop gets executed!
Rahul said:
9 years ago
Nice programming question.
Jayashree said:
9 years ago
Guys! what is the first condition?
What is the connection between loop, x and also cases?
What is the connection between loop, x and also cases?
Rimsha Fazal said:
9 years ago
I didn't understand how the conditions are matching in the program. There are simple values given by z in every iteration which is executed and printed what's the roll of x and y then?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers