Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 106
Question ID: UKOCP50130
What will be the output of compiling and executing the Test class?
- package com.udayankhattry.ocp;
- public class Test {
- public static void main(String[] args) {
- int a = 5;
- int x = 10;
- switch(x) {
- case 10:
- a *= 2;
- case 20:
- a *= 3;
- case 30:
- a *= 4;
- }
- System.out.println(a);
- }
- }
- A 5
- B 10
- C 30
- D 120
Reveal correct answer
Correct answer: D
Explanation
UKOCP50130:
Matching case block 'case 10:' is found, a *= 2; is executed, which means a = a * 2; => a = 5 * 2; => a = 10;
No break statement, hence it enters in fall-through.
a *= 3; is executed, which means a = a * 3; => a = 10 * 3; => a = 30;
a *= 4; is executed, which means a = a * 4; => a = 30 * 4; => a = 120;
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.
