Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 8
Consider the code:
- public class Test
- {
- public static void main(String[] args)
- {
- int x =10;
- int y =0;
- switch(x)
- {
- case 10:
- y++;
- case 20:
- y=y+2;
- case 30:
- y= y+3;
- }
- System.out.println(y);
- }
- }
What is the result?
-
A
2
-
B
1
-
C
6
-
D
0
Reveal correct answer
Correct answer: C
Explanation
Within the switch statement if any case is matched then from that case onwards all statements will be executed until break or end of the switch. This is called fall-through inside switch.Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
