public class Foo {
public static void main(String[] args) {
for (var i = 0; i < 10; i++) {
switch (i % 5) {
case 2:
i *= 2 * i;
break;
case 3:
i++;
break;
case 1:
case 4:
i++;
continue;
default:
break;
}
System.out.print(i + " ");
i++;
}
}
}
// Result:
// 0 8
E is correct!
i = 0, 0%5 = 0, default get's hit. 0 get's printed and incremented to 1.
i = 1, case 1 has no break so we increment i to 2 and continue.
i=2, 2%5 = 2 so 2 * 2 *2= 8. we print 8 and increment to 9
i=9, 9%5 = 4. we increment i to 10 and continue.
We exit the for loop as 10 is not smaller than 10.
i=0 -> 0%5 is 0 so we go to default and break, then print 0 and increment it, so the next iteration will have i = 2.
I=2 -> 2%5 is 2 so we double I then break from the loop with I = 4 print 4, increment to 5 and continue.
I=6 -> 6%5 is 1 so we just increment and continue (go to the next iteration)
I=8 -> 8%5 is 3 so we just increment to 9 and break, print 9 then increment and reach 10 which is the end.
So the answer is D.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
APJ1
6 months, 2 weeks agojames2033
9 months, 1 week ago[Removed]
11 months, 1 week agoLebannin
1 year agoStavok
1 year, 2 months agoAlanRM
1 year, 3 months agopikosss
1 year, 4 months agoAnkit1010
1 year, 6 months ago