MCQ

Java MCQ – Enumerations

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Enumerations”.
 

1. If we add Enum constants to a TreeSet, what sort order will it use?

A Sorted by sort() method

B Sorted in the order of declaration in Enums

C Sorted alphabetically by Enums

D Sorted in descending order

B
TreeSet will sort the values in the order in which the Enum constants are declared.

 

 

2. What is the order of the variables in Enum?

A Descending order

B Ascending order

C Random order

D Depends on sort() method

B
The compareTo() method is implemented in Enum. java.lang.Enum.compareTo() sort variables in ascending order.

 

 

3. Can we create an instance of Enum outside of Enum itself?

A True

B False

B
Enum does not have a public constructor.

 

 

4. Which method returns the elements of the Enum class?

A getEnum()

B getEnumList()

C getEnums()

D getEnumConstants()

D
The method getEnumConstants() returns the elements of the class enum or null if the object of the Class does not represent an enum type.

 

 

5. From which class do all Enums extend?

A Enums

B Enum

C EnumClass

D Object

B
All enums implicitly extend from java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

 

 

6. Are the Enums “type-safe”?

A True

B False

A
Enumerations are safe, because they have their own namespace.

 

 

7. What is the output of this program?
enum Color {
        Bleu, Rouge, Vert, Blanc
};

System.out.println(Color.Bleu.ordinal());

A 0

B 1

C 2

D 3

A
The ordinal() method allows you to find the sequence number of an element defined in Enum.

 

 

8. What is the output of this program?
class Word
{
 
}
 
enum Enums extends Word
{
    AAA, BBB, CCC, DDD;
}

A Compilation error

B Runtime error

C Exception EnumNotDefined

D It works well

A
Enum types cannot inherit a class.

 

 

9. What is the output of this program?
enum Levels 
{
    private machinA,
 
    public machinB,
 
    protected machinC;
}

A Compilation error

B Runtime error

C Exception EnumNotDefined

D It works well

A
Enum cannot have an access modifier. They are public, static and are final by default.

 

 

10. What is the output of this program?
enum Enums
{
    machinX, machinY, machinZ;
 
    private Enums()
    {
        System.out.println(1);
    }
}
 
public class ClassMain
{
    public static void main(String[] args)
    {
        Enum en = Enums.machinY;
    }
}

A Compilation error

B Runtime error

C 1

D Exception

C
Enums constructor is called and displays 1.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *