Menu
Question Index
...

What will be the output of the following program?

public class SunMoonStars {
    public static void main(String[] args) {
        String bodies[] = {"Sun", "Moon", "Earth", "Pluto"};
        for (String body : bodies) {
            print(Planets.valueOf(body));
        }
    }
    private static void print(ValueProvider v) {
        if (v instanceof Planets) {
            System.out.print("#" + v.getValue());
        } else {
            System.out.print("@@@@");
        }
    }
}
interface ValueProvider {
    String getValue();
}
enum Planets implements ValueProvider {
    Sun, Earth, Mars, Venus;
    public String getValue() { return toString(); }
}


#Sun#Earth
#Sun@@@@#Earth@@@@
Compilation Error because enum can not implement interfaces
Compilation Error for some other reason
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