Menu
Question Index
...

What will be the output of the following program?

public class AnimalsMeeting {
    public static void main(String[] args) {
        String lion = "Lion";
        String deer = "Deer";
        String donkey = "Donkey";
        String monkey = "Monkey";
        String allAnimals = "All Animals";
        int number = 0;
        switch (number) {
            case 0 :    System.out.println(allAnimals + " are :");
            case 1 :
                int i = 1;
                System.out.println(i + ". " + lion);
                if (number != 0) {    break;    }
            case 2 :
                int i = 2;
                System.out.println(i + ". " + deer);
                if (number != 0) {    break;    }
            case 3 :
                int i = 3;
                System.out.println(i + ". " + donkey);
                if (number != 0) {    break;    }
            case 4 :
                int i = 4;
                System.out.println(i + ". " + monkey);
                break;
            default :    System.out.println("No Animals are present");
        }
    }
}


All Animals are :
All Animals are :
4. Monkey
All Animals are :
1. Lion
2. Deer
3. Donkey
4. Monkey
All Animals are :
1. Lion
2. Deer
3. Donkey
4. Monkey
No Animals are present
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: switch Statement In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App