C#.NET enums — given: enum per { married, unmarried, divorced, spinster } per.married = 10; Console.WriteLine(per.unmarried); Which statement describes the program's behavior?

Difficulty: Easy

Correct Answer: The program will report an error since an enum element cannot be assigned a value outside the enum declaration.

Explanation:

Introduction / Context:Enum members in C# are named constants, not variables. This question assesses understanding of enum immutability and default value assignment.

Given Data / Assumptions:

  • per has default underlying type int and default values 0, 1, 2, 3 for the listed members.
  • A subsequent line attempts per.married = 10;

Concept / Approach:Enum members are compile-time constants. You cannot assign to them at runtime or change their values outside of the enum declaration. Such an assignment is a compile-time error.

Step-by-Step Solution:

The compiler parses per.married = 10; and flags an error because per.married is a constant, not a writable l-value.Since compilation fails, no runtime output occurs.

Verification / Alternative check:To assign explicit values, you must do so inside the enum declaration, e.g., married = 10.

Why Other Options Are Wrong:They assume the program runs and prints numeric values. It will not compile due to the invalid assignment.

Common Pitfalls:Confusing enum constants with static fields; they are not mutable fields.

Final Answer:The program will report an error since an enum element cannot be assigned a value outside the enum declaration.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion