C#.NET enums — analyze the following declaration: enum color : byte { red = 500, green = 1000, blue = 1500 } Which statement about this code is correct?

Difficulty: Easy

Correct Answer: Since 500, 1000, 1500 exceed the valid range of byte, the compiler will report an error.

Explanation:

Introduction / Context:In C#, an enum’s underlying type controls the representable range of its named constants. This question checks if you remember that constraint.

Given Data / Assumptions:

  • The enum underlying type is byte (range 0..255).
  • Assigned values are 500, 1000, 1500.

Concept / Approach:Enum members must be compile-time constants convertible to the underlying type without overflow. Assigning a value outside the underlying type’s range is a compile-time error.

Step-by-Step Solution:

Check range: 500, 1000, 1500 > 255 → not representable as byte.Compiler emits an error for each out-of-range assignment.

Verification / Alternative check:Change the underlying type to int or reduce constants to within 0..255 to make it compile.

Why Other Options Are Wrong:(a) is false — you can assign compatible constants; (b) successive values are not required; (d) underlying type can be any integral type except char by default (int if unspecified); (e) access modifiers on enum members are not used — they are constants.

Common Pitfalls:Assuming the compiler silently wraps values; it does not for enum member declarations.

Final Answer:Since 500, 1000, 1500 exceed the valid range of byte, the compiler will report an error.

Discussion & Comments

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