Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - Finding the output (Q.No. 4)
4.
What will be the output of the program?
public class SwitchTest
{
public static void main(String[] args)
{
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x)
{
int j = 1;
switch (x)
{
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
Answer: Option
Explanation:
Because there are no break statements, once the desired result is found, the program continues though each of the remaining options.
Discussion:
14 comments Page 1 of 2.
Thg said:
1 decade ago
int x=4
then cm to switch statement x=4 .
case 4 will we executed j++ =2
case 5 j++ =3
return j+x
3+4 =7
the result is 7..
how 8 come
then cm to switch statement x=4 .
case 4 will we executed j++ =2
case 5 j++ =3
return j+x
3+4 =7
the result is 7..
how 8 come
Ishrat said:
1 decade ago
Please someone explain this problem how 8 comes ?
Aashish said:
1 decade ago
First it satisfies the condition x=4
then it jumps to case 4, and then because of no break statements,
it executes all statements..
So, in case 4 , j becomes 1
in case 5 , j becomes 2
in case dafault , j becomes 3
But there is a post increment operator so it increments the value
of j to 4 after the default case executes..
and then it returns 4+4=8 value...
Hope you understand...
then it jumps to case 4, and then because of no break statements,
it executes all statements..
So, in case 4 , j becomes 1
in case 5 , j becomes 2
in case dafault , j becomes 3
But there is a post increment operator so it increments the value
of j to 4 after the default case executes..
and then it returns 4+4=8 value...
Hope you understand...
Senthil kumar said:
1 decade ago
When you are consider this program
The actual value of "J" is 1 and "X"
Position is 4 so,the 4th position of "J" value is 5 and default Place does not have and break .
So that position also increment so eventually result of program is
4th place value=5
5th place
value=6
default
value=7
j=1 as per intial
So, total = 7+1 = 8.
The actual value of "J" is 1 and "X"
Position is 4 so,the 4th position of "J" value is 5 and default Place does not have and break .
So that position also increment so eventually result of program is
4th place value=5
5th place
value=6
default
value=7
j=1 as per intial
So, total = 7+1 = 8.
Ashwini Surya said:
1 decade ago
Line no 5 SwitchIt(4) declare the value of SwitchIt, Then in line 7 SwithchIt defines x so 4 is the value indicate to x.
Then, at line 10 switch(x),x value is 4 so loop go to at case 4 and it increase by 1 at every case so there are three cases on that time y=3 and x=4 but there is declare the value y=1 means y's value is 4.
So answer will be 8.
Then, at line 10 switch(x),x value is 4 so loop go to at case 4 and it increase by 1 at every case so there are three cases on that time y=3 and x=4 but there is declare the value y=1 means y's value is 4.
So answer will be 8.
Srikanth said:
1 decade ago
@Aashish, thank you.
Shanmukasrikanth said:
1 decade ago
Here x=4 and j=1 when ever the switch is executed. The control goes to case 4. So in case 4 value of j=2 and their is no break statement in the cases so remaining. Cases will execute as it is so in case 5 value of j=3 in default value of j=4. And at last the function is of type. Int so it return some value. So it returns x+j i.e. 4+4=8. That's it.
Kunal said:
1 decade ago
Hopefully everyone has understood till default case value of j=3.
Once it comes out of switch case postincrement of j is there (i.e. value of j=3 but j++ is there so j becomes 4).
So finally value will be x+j = 4+4 = 8.
So answer will be 8.
Once it comes out of switch case postincrement of j is there (i.e. value of j=3 but j++ is there so j becomes 4).
So finally value will be x+j = 4+4 = 8.
So answer will be 8.
Unknown 21 said:
1 decade ago
Our condition is x=4;
Now, move to case 4, where x=4 and j=1 (a/c to loop , but after the complete execution of j++ in case 4, j becomes 2 (due to post increment i.e, ++).
Case 5: Starts with j=2; and after complete execution of j++ in case 5, j becomes 3.
Now, default takes j=3, and after execution of j++, j becomes 4.
And at return:x=4 and j=4; which gives 4+4=8.
Now, move to case 4, where x=4 and j=1 (a/c to loop , but after the complete execution of j++ in case 4, j becomes 2 (due to post increment i.e, ++).
Case 5: Starts with j=2; and after complete execution of j++ in case 5, j becomes 3.
Now, default takes j=3, and after execution of j++, j becomes 4.
And at return:x=4 and j=4; which gives 4+4=8.
Varooon said:
10 years ago
Default : j++; will also will be executed, Because of the Absence of "break" statement. This is the key thing to note here.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers