Menu
Topics Index
...
`

Add Numbers to EnumMap2136


If you need explanation Read this topic

If you need Answer Take test on this topic

Add Numbers to EnumMap2136
What will be the output of the following program?
import java.util.*;
public class AddNumbers {
    enum NUMBERS {
        One, Two, Three, Four;
    }
    public static void main(String[] args) {
        EnumMap<NUMBERS, Integer> em = new EnumMap<>(NUMBERS.class);
        em.put(NUMBERS.Two, 2);
        em.put(NUMBERS.One, 1);
        em.put(NUMBERS.Three, 3);
        em.put(NUMBERS.Four, 4);
        em.put(NUMBERS.Three, 5);
        System.out.println(em);
    }
}

A. {One=1, Two=2, Three=3, Four=4, Three=5}
B. {One=1, Two=2, Four=4, Three=5}
C. {One=1, Two=2, Three=3, Four=4}
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java EnumMap

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.

Answer is D...the output is {One=1, Two=2, Three=5, Four=4} bcoz.. the integervalue for Three is overwritten..and becomes 5..so the output is {One=1, Two=2, Three=5, Four=4}

Posted by Sneha Rapool    2014-04-30 14:14:31


e

Posted by Ruha Smilee    2014-04-30 14:30:41


Ans is D as Enum serves as a type of fixed number of constants and can be used at least for two things and we put the numbers to enum type variables and three is overwritten as 5 so other output is obtained

Posted by Bharath Yelchuri    2014-05-01 04:57:31


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

Posted by Merit Campus    2014-05-01 06:59:25


© meritcampus 2019

All Rights Reserved.

Open In App