Challenge - 5 Problems
Enum vs Constants Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enum and constant usage
What is the output of this C# program that uses both an enum and constants?
C Sharp (C#)
using System; class Program { enum Status { Active = 1, Inactive = 0 } const int ActiveConst = 1; const int InactiveConst = 0; static void Main() { Console.WriteLine(Status.Active == ActiveConst); Console.WriteLine(Status.Inactive == InactiveConst); Console.WriteLine((int)Status.Active + (int)Status.Inactive); } }
Attempts:
2 left
💡 Hint
Remember enums can be cast to their underlying integer values.
✗ Incorrect
Enums in C# have underlying integer values. Comparing enum members to constants with the same values returns true. Casting enums to int sums their values correctly.
🧠 Conceptual
intermediate1:30remaining
Choosing between enum and constants
Which scenario is best suited for using an enum instead of constants in C#?
Attempts:
2 left
💡 Hint
Enums group related values under one type.
✗ Incorrect
Enums are best for grouping related named constants with distinct values. Constants are better for single or unrelated values.
❓ Predict Output
advanced1:30remaining
Output of invalid enum assignment
What error or output occurs when this C# code runs?
C Sharp (C#)
using System; enum Colors { Red = 1, Green = 2, Blue = 3 } class Program { static void Main() { Colors c = (Colors)5; Console.WriteLine(c); } }
Attempts:
2 left
💡 Hint
Casting an integer outside enum values is allowed but may produce unexpected output.
✗ Incorrect
Casting an integer not defined in enum prints the integer value as enum name is missing.
❓ Predict Output
advanced1:00remaining
Output of constant string concatenation
What is the output of this C# code using constants?
C Sharp (C#)
using System; class Program { const string Hello = "Hello"; const string World = "World"; static void Main() { string message = Hello + ", " + World + "!"; Console.WriteLine(message); } }
Attempts:
2 left
💡 Hint
Look carefully at the commas and spaces in the concatenation.
✗ Incorrect
The string concatenation includes a comma and space after Hello, so output is 'Hello, World!'.
🧠 Conceptual
expert2:30remaining
Why prefer enums over constants for state management?
Why is using enums generally better than constants for managing states in C# applications?
Attempts:
2 left
💡 Hint
Think about how enums help prevent invalid values.
✗ Incorrect
Enums restrict values to defined set, improving type safety and readability. Constants are just fixed values without grouping or type safety.