Given the code fragment:
String color = "teal";
switch (color) {
case "Red":
System.out.println("Found Red");
case "Blue":
System.out.println("Found Blue");
break;
case "Teal":
System.out.println("Found Teal");
break;
default:
System.out.println("Found Default");
}
What is the result?
A.
Found Red
Found Default
Found Default
B.
Found Teal
C.
Found Red
Found Blue
Found Teal
Found Blue
Found Teal
D.
Found Red
Found Blue
Found Teal
Found Default
Found Blue
Found Teal
Found Default
E.
Found Default
題解
switch內可以使用「break」關鍵字來使程式立刻跳出switch。在這題目中,要判斷的字串為「teal」,雖然有「Teal」的case,但它們「T」的大小寫不符合,因此switch只會執行到第21~22行,輸出「Found Default」。