21). What is the order of variables in Enum?
[A]Ascending order
[B]Descending order
[C]Random order
[D]Depends on the order() method
Show Answer
Correct Answer: Depends on the order() method
Notes:
Answer: d
Explanation: The order of appearance of variables in Enum is their natural order (the natural order means the order in which they are declared inside Enum type). However, the compareTo() method is implemented to order the variable in ascending order.
22). Can we create an instance of Enum outside of Enum itself?
[A]True
[B]False
[C]none
[D]none
Show Answer
Correct Answer: False
Notes:
Answer: b
Explanation: No, instances of Enum cannot be created outside of Enum boundary, because Enum does not have a public constructor
23). Can we create an instance of Enum outside of Enum itself?
[A]True
[B]False
[C]none
[D]none
Show Answer
Correct Answer: False
Notes:
Answer: b
Explanation: No, instances of Enum cannot be created outside of Enum boundary, because Enum does not have a public constructor
24). What will be the output of the following Java code?
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
Show Answer
Correct Answer: 0
Notes:
Answer: a Explanation: ordinal() method provides number to the variables defined in Enum.
25). If we try to add Enum constants to a TreeSet, what sorting order will it use?
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
[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
Show Answer
Correct Answer: Sorted in the order of declaration of Enums
Notes:
Answer: a
Explanation: Tree Set will sort the values in the order in which Enum constants are declared.
26). What will be the output of the following Java 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
Show Answer
Correct Answer: Compilation Error
Notes:
Answer: b
Explanation: Enum types cannot extend class.
27). What will be the output of the following Java code snippet?
enum Levels
{
private TOP,
public MEDIUM,
protected BOTTOM;
}
[A]Runtime Error
[B]EnumNotDefined Exception
[C]It runs successfully
[D]Compilation Error
Show Answer
Correct Answer: Compilation Error
Notes:
Answer: d
Explanation: Enum cannot have any modifiers. They are public, static and final by default.
28). What will be the output of the following Java 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
Show Answer
Correct Answer: 10 10 10
Notes:
Answer: a
Explanation: The constructor of Enums is called which prints 10.
29). Which method returns the elements of Enum class?
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]getEnums()
[B]getEnumConstants()
[C]getEnumList()
[D]getEnum()
Show Answer
Correct Answer: getEnumConstants()
Notes:
Answer: b
Explanation: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type.
30). Which class does all the Enums extend?
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]Object
[B]Enums
[C]Enum
[D]EnumClass
Show Answer
Correct Answer: Enum
Notes:
Answer: c
Explanation: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
31). Are enums are type-safe?
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]True
[B]False
[C]none
[D]none
Show Answer
Correct Answer: True
Notes:
Answer: a
Explanation: Enums are type-safe as they have own name-space.