Question on Data Type-Enums



Question on Data Type-Enums

1. What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) depends on the order() method

Answer: a
Explanation: The compareTo() method is implemented to order the variable in ascending order.

2. Can we create an instance of Enum outside of Enum itself?
a) True
b) False

Answer: b
Explanation: Enum does not have a public constructor.

3.
 enum Season {
        WINTER, SPRING, SUMMER, FALL
    };
    System.out.println(Season.WINTER.ordinal());
a) 0
b) 1
c) 2
d) 3

Answer: a
Explanation: ordinal() method provides number to the variables defined in Enum.

4. If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums

Answer: a
Explanation: Tree Set will sort the values in the order in which Enum constants are declared.

5. What is the output of below code snippet?
class A
{

}

enum Enums extends A
{
    ABC, BCD, CDE, DEF;
}
a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception

Answer: b
Explanation: Enum types cannot extend class.

6. What is the output of below code snippet?
 enum Levels
{
    private TOP,

    public MEDIUM,

    protected BOTTOM;
}
a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error

Answer: d
Explanation: Enum cannot have any modifiers. They are public, static and final by default.

7. What is the output of below code snippet?
enum Enums
{
    A, B, C;

    private Enums()
    {
        System.out.println(10);
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        Enum en = Enums.B;
    }
}
a)
   10
   10
   10
b) Compilation Error
c)
   10
   10
d) Runtime Exception

Answer: a
Explanation: The constructor of Enums is called which prints 10.

8. Which method returns the elements of Enum class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()

Answer: b
Explanation: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type.

9. Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass

Answer: c
Explanation: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

10. Are enums are type-safe?
a) True
b) False

View Answer
Answer: a
Explanation: Enums are type-safe as they have own name-space.
Question on Data Type-Enums Question on Data Type-Enums Reviewed by Unknown on February 24, 2019 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.