This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-Enums”.
1. What is the order of variables in Enum? Answer: a 2. Can we create an instance of Enum outside of Enum itself? Answer: b 3. What will be the output of the following Java code? a) 0 Answer: a 4. If we try to add Enum constants to a TreeSet, what sorting order will it use? Answer: a 5. What will be the output of the following Java code snippet? a) Runtime Error Answer: b 6. What will be the output of the following Java code snippet? a) Runtime Error Answer: d 7. What will be the output of the following Java code snippet? a) b) Compilation Error d) Runtime Exception Answer: a 8. Which method returns the elements of Enum class? Answer: b 9. Which class does all the Enums extend? Answer: c 10. Are enums are type-safe? Answer: a
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
Clarification: The compareTo() method is implemented to order the variable in ascending order.
a) True
b) False
Clarification: Enum does not have a public constructor.
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
b) 1
c) 2
d) 3
Clarification: ordinal() method provides number to the variables defined in Enum.
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
Clarification: Tree Set will sort the values in the order in which Enum constants are declared.
class A
{
}
enum Enums extends A
{
ABC, BCD, CDE, DEF;
}
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
Clarification: Enum types cannot extend class.
enum Levels
{
private TOP,
public MEDIUM,
protected BOTTOM;
}
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error
Clarification: Enum cannot have any modifiers. They are public, static and final by default.
enum Enums
{
A, B, C;
private Enums()
{
System.out.println(10);
}
}
public class MainClass
{
public static void main(String[] args)
{
Enum en = Enums.B;
}
}
10
10
10
c) 10
10
Clarification: The constructor of Enums is called which prints 10.
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()
Clarification: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type.
a) Object
b) Enums
c) Enum
d) EnumClass
Clarification: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
a) True
b) False
Clarification: Enums are type-safe as they have own name-space.
