Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 110
Question ID: UKOCP26028
What will be the result of compiling and executing Test class?
- package com.udayankhattry.ocp;
- public class Test {
- public static void main(String[] args) {
- String sport = "swimming";
- switch (sport) {
- default:
- System.out.println("RUNNING");
- case "Tennis":
- System.out.println("TENNIS");
- case "Swimming":
- System.out.println("SWIMMING");
- case "Football":
- System.out.println("FOOTBALL");
- break;
- }
- }
- }
- A RUNNING
- B SWIMMING
-
C
SWIMMING
FOOTBALL
-
D
RUNNING
TENNIS
SWIMMING
FOOTBALL
Reveal correct answer
Correct answer: D
Explanation
UKOCP26028:
"swimming" is different from "Swimming", so there is no matching case available. default block is executed, "RUNNING" is printed on to the console. No break statement inside default, hence control enters in fall-through and executes remaining blocks until the break; is found or switch block ends. So in this case, it prints TENNIS, SWIMMING, FOOTBALL one after another and break; statement takes control out of switch block. main method ends and program terminates successfully.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
