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.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:
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