Test your understanding of Switch statement in java :
Time limit: 0
Quiz-summary
0 of 1 questions completed
Questions:
1
Information
package quiz;
public class Quiz23 {
public static void main(String[] args) {
int x =0;
int[] nums= {1,2,3,5};
for (int i:nums)
switch(i){
case 1:
x += i;
case 5:
x += i;
default:
x += i;
case 2:
x += i;
}
System.out.println(x);
}
}
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
Time has elapsed
Categories
Not categorized0%
1
Answered
Review
Question 1 of 1
1. Question
package quiz;
public class Quiz23 {
public static void main(String[] args) {
int x =0;
int[] nums= {1,2,3,5};
for (int i:nums)
switch(i){
case 1:
x += i;
case 5:
x += i;
default:
x += i;
case 2:
x += i;
}
System.out.println(x);
}
}
Select correct answer from options below:
Correct
The code is legal.
When a case is matched in switch, its code and all the subsequent case code will run unless break is encountered.
Switch statement can be added inside for each loop and the case statement can be present in any order.
Incorrect
The code is legal.
When a case is matched in switch, its code and all the subsequent case code will run unless break is encountered.
Switch statement can be added inside for each loop and the case statement can be present in any order.
But it’s illogical. For example, why it still takes all cases when I=5? It should consider order of cases, shouldn’t it? If we hit 5, we should not hit 1 and possibly should hit 2. I can’t see a reason.
In this quiz, we have a switch inside a for-each loop. This is allowed in java. Since we have 4 elements in nums, it the switch will be evaluated 4 times. For first time, i is checked for 1 and match is found in case 1. So, x =1.But since there is no break statement, it will fall through and i will be 4 when it comes out of switch. Similarly, the outputs on next iterations are 6, 12 and 27. 27 is the value of x after final loop. Let us know if you still have confusions.
But it’s illogical. For example, why it still takes all cases when I=5? It should consider order of cases, shouldn’t it? If we hit 5, we should not hit 1 and possibly should hit 2.
#
But it’s illogical. For example, why it still takes all cases when I=5? It should consider order of cases, shouldn’t it? If we hit 5, we should not hit 1 and possibly should hit 2. I can’t see a reason.
#
I couldn’t understand the result of Quiz 23.Can you give me a clear explanation?
#
Hi.. let me try to explain it..
In this quiz, we have a switch inside a for-each loop. This is allowed in java.
Since we have 4 elements in nums, it the switch will be evaluated 4 times.
For first time, i is checked for 1 and match is found in case 1. So, x =1.But since there is no break statement, it will fall through and i will be 4 when it comes out of switch.
Similarly, the outputs on next iterations are 6, 12 and 27. 27 is the value of x after final loop.
Let us know if you still have confusions.
Thanks for visiting.
#
But it’s illogical. For example, why it still takes all cases when I=5? It should consider order of cases, shouldn’t it? If we hit 5, we should not hit 1 and possibly should hit 2.
#
#