Topics Index

Enum class

If you need explanation Read this topic

If you need Answer Take test on this topic

Enum class
What will be the output of the following program?
public class CricketIsDivine {
    public static void main(String[] args) {
        int i = 0;
        for (i = 5; i < 20; i += 5) {
            System.out.print(CricketingGods.lookup(i) + "*");
        i = (i / 9) * 9;
        while (i < 100) {
            i += 9;
            if (CricketingGods.lookup(i) != null)
                        System.out.print(CricketingGods.lookup(i) + "#");
enum CricketingGods {
    Sachin(99), Dravid(15), Dhoni(10), Kohli(5), Raina(27), Ganguly(54);
    int jersey;
    CricketingGods(int jersey) {
        this.jersey = jersey;
    static CricketingGods lookup(int jersey) {
        for (CricketingGods god : values()) {
            if (god.jersey == jersey) {
                return god;
        return null;

A. Kohli*Dhoni*Dravid*Raina#Ganguly#Sachin#
B. Kohli*Dhoni*Dravid*null#null#null#
C. Kohli*Dhoni*Dravid*Raina#null#null#Ganguly#null#null#null#null#Sachin#null#
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Enumeration

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is D. Output is Kohli#Dhoni#Dravid#
for the first iteration i value is 5 so Kohli (with jersey 5) is printed and jersey is incremented next i is incremented by 5 and for the value 10 Dhoni is printed. Finally for 15 Dravid is printed. If null is returned by CricketingGods, it is not printed since if condition is not satisfied.

Posted by Nikhil Sulibhavi    2014-04-29 13:01:30

ANs is A.. first kohli-5 dhoni 10 dravid vl be returnd by lookup function and then raina-27 and ganguly-54 sachin-99 vl be returnd and gets printd..

Posted by Om Prakash Om    2014-04-29 13:03:29

ans is B,since after successful lookup their jersey value is incrementd and returns the item ,here inside for loop  there is only one lookup call then for loop iterates  3 items and prints Kohli*Dhoni*Dravid* then in while loop it have two lookup calls one will check it is there r not(null or notnull) and it increments jersey value then second call can not find  the item with that jersey value so it prints null

Posted by Raviteja Daggupati    2014-04-29 14:28:09

Congratulations Raviteja Daggupati. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-30 04:11:26

© meritcampus 2019

All Rights Reserved.

Open In App