Topics Index

People names2315

If you need explanation Read this topic

If you need Answer Take test on this topic

People names2315
What will be the output of the following program?
public class People {
    public static void main(String[] args) {
        for (NAMES type : NAMES.values()) {
            switch (type) {
                default :
                    System.out.print(type.str + " ");
enum NAMES {
    KIRAN("Kiran"), ASHOK("Ashok"), DHARMA("Dharma"), OLETI("Oleti");
    String str;
    NAMES(String str) {
        this.str = str;

B. Kiran Ashok Dharma Oleti
C. Some other output
D. Output can not be determined
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 B

Here Enum is used.An enum type is a special data type that enables for a variable to be a set of predefined constants.You should use enum types any time you need to represent a fixed set of constants.

In Main() method for loop is used with counter name as type whose type is enum.In switch case type is used as expression as there are no cases in switch case therefore control will go in default block.In default block,type.str is printed i.e one by one vaues will get printed(Kiran Ashok,Dharma,Oleti)

Posted by Mânïshå Mùlchåndânï    2014-12-31 09:21:38

ans b

Java enums are a special Java type used to define collections of constants. An enum type is a special kind of Java class. It can contain constants, methods etc.

You can obtain an array of all the possible values of an enum type by calling its static values() method. All enum types get a static values() method automatically by the Java compiler.

coming to this program here enum NAMES declared with 4 elements.
thn for loop used to print all enum values
in switch we passed enum value ,in the given order.

in switch we have only default option

for loop iterates 4 times.

1st time  sop prints "Kiran "

2nd time  sop prints "Ashok "

3rd time  sop prints "Dharma "

4th time  sop prints "Oleti "

Posted by Maheshwari Natarajan    2014-12-31 11:55:55

This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-03 03:47:44

© meritcampus 2019

All Rights Reserved.

Open In App