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 variable a
is initialized with the value 2.
-
Switch Statement:
switch (a) {
The switch
statement checks the value of a
:
-
Case 1:
Since a
is 2, it does not match case 1
, so the code inside case 1
is not executed.
-
Case 2:
The value of a
matches case 2
, so the statement inside case 2
is executed:
System.out.print("Deer");
This prints Deer.
-
Default Case:
Since there is no break
statement, the execution falls through to the default
case:
System.out.print("Lion");
This prints Lion.
-
Output:
The output of the program is:
DeerLion
Conclusion:
The correct answer is: c) DeerLion