C# Enum MCQ

1. What is an enum in C#?

a) A method that returns several values
b) A user-defined data type that consists of integral constants
c) A special type of class for storing collections
d) A data structure similar to an array

Answer:

b) A user-defined data type that consists of integral constants

Explanation:

An enum (enumeration) in C# is a value type defined by a set of named integral constants.

2. What is the default underlying type of an enum in C#?

a) int
b) short
c) long
d) byte

Answer:

a) int

Explanation:

In C#, the default underlying type of an enum is 'int'.

3. How can you define an enum in C# with a different underlying type?

a) enum MyEnum : byte { Value1, Value2 }
b) enum MyEnum { Value1, Value2 } as byte
c) byte enum MyEnum { Value1, Value2 }
d) enum byte MyEnum { Value1, Value2 }

Answer:

a) enum MyEnum : byte { Value1, Value2 }

Explanation:

The underlying type of an enum can be specified after the colon in the enum declaration.

4. Can an enum in C# contain duplicate values?

a) Yes
b) No
c) Only if they are of different types
d) Only if they are explicitly allowed

Answer:

b) No

Explanation:

Enums in C# cannot contain duplicate values. Each value in an enum must be unique.

5. How do you assign a specific value to an enum member in C#?

a) enum MyEnum { Value1 = 1, Value2 = 2 }
b) enum MyEnum { Value1(1), Value2(2) }
c) enum MyEnum { 1, 2 }
d) enum MyEnum { Value1, Value2 } = { 1, 2 }

Answer:

a) enum MyEnum { Value1 = 1, Value2 = 2 }

Explanation:

Specific values can be assigned to enum members by using the equals sign followed by the value.

6. What is the first value of an enum in C# if no value is explicitly assigned?

a) 0
b) 1
c) null
d) It generates a compile-time error

Answer:

a) 0

Explanation:

If no value is explicitly assigned to the first member of an enum, its value defaults to 0.

7. How can you convert an enum to its corresponding numeric value?

a) (int)MyEnum.Value1
b) Convert.ToInt32(MyEnum.Value1)
c) MyEnum.Value1.ToInt()
d) Both a and b

Answer:

d) Both a and b

Explanation:

An enum can be cast to its underlying type or converted using the Convert class.

8. How do you define a flag enum in C#?

a) enum MyEnum { Value1, Value2 } with flags
b) enum MyEnum { Value1 = 1, Value2 = 2 } as flags
c) [Flags] enum MyEnum { Value1 = 1, Value2 = 2 }
d) enum MyEnum : Flags { Value1, Value2 }

Answer:

c) [Flags] enum MyEnum { Value1 = 1, Value2 = 2 }

Explanation:

A flag enum is defined by applying the [Flags] attribute and usually assigning values as powers of two.

9. Can you use strings as enum values in C#?

a) Yes
b) No
c) Only with the [Strings] attribute
d) Only in a special type of enum

Answer:

b) No

Explanation:

Enums in C# must be integral types; string values are not allowed.

10. What happens when you try to parse an invalid string to an enum in C#?

a) It returns null
b) It defaults to the first enum value
c) It throws an exception
d) It ignores the invalid value

Answer:

c) It throws an exception

Explanation:

Attempting to parse an invalid string to an enum using Enum.Parse will result in a runtime exception.

Leave a Comment

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

Scroll to Top