C#.NET enums — given: enum per { married, unmarried, divorced, spinster } per.married = 10; Console.WriteLine(per.unmarried); Which statement describes the program's behavior?
-
AThe program will output a value 11.
-
BThe program will output a value 1.
-
CThe program will output a value 2.
-
DThe program will report an error since an enum element cannot be assigned a value outside the enum declaration.
-
EThe enum elements must be declared private.
Answer
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:
perhas default underlying typeintand 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 parsesper.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.