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

Choose the Best Option

Click any option to instantly check if you're correct.

  • A Tiger
  • B Lion
  • C DeerLion
  • D No output
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:

  1. Initialization:

    int a = 2;
    The variable a is initialized with the value 2.

  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.

  3. Output:

    The output of the program is:
    DeerLion

Conclusion:

The correct answer is: c) DeerLion

No Previous Next Question

Share This Question

Challenge a friend or share with your study group.