Menu
Question Index
...

What will be the output of the following program?

public class EnumerateIt {
    public static void main(String[] args) {
        for (A a : A.values()) {
            System.out.print(a.name() + " " + a.ordinal() + " ");
            try {
                B b = B.valueOf(a.name());
                System.out.print(b.name() + " " + b.ordinal() + " ");
            } catch (Exception e) {
                System.out.print(a.name() + " " + a.ordinal() + " ");
            }
        }
    }
}
enum A {
    A, B, C
}
enum B {
    B, C, D
}


A 0 A 0 B 1 B 0 D 2 D 2
A 0 A 0 B 1 B 1 C 2 C 2
A 0 A 0 B 1 B 0 C 2 C 1
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Enumeration

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App