Single Choice
Easy
QWhat is the output of the java program below
int a = 2;
switch (a) {
case 1:
System.out.print("Tiger");
case 2:
System.out.print("Deer");
default:
System.out.print("Lion");
}
ID: #22224
switch case in Java
124 views
Question Info
#22224Q ID
EasyDifficulty
switch case in JavaTopic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer
Explanation
Let's analyze the given Java program to determine its output.
Code:
int a = 2;
switch (a) {
case 1:
System.out.print("Tiger");
case 2:
System.out.print("Deer");
default:
System.out.print("Lion");
}
Explanation:
-
Initialization:
int a = 2;
The variableais initialized with the value 2. -
Switch Statement:
switch (a) {
Theswitchstatement checks the value ofa:-
Case 1:
Since
ais 2, it does not matchcase 1, so the code insidecase 1is not executed. -
Case 2:
The value of
amatchescase 2, so the statement insidecase 2is executed:System.out.print("Deer");
This prints Deer. -
Default Case:
Since there is no
breakstatement, the execution falls through to thedefaultcase:System.out.print("Lion");
This prints Lion.
-
-
Output:
The output of the program is:
DeerLion
Conclusion:
The correct answer is: c) DeerLion
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic