0
0
C Sharp (C#)programming~20 mins

Enum vs constants decision in C Sharp (C#) - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum vs Constants Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A
False
True
1
B
True
False
1
C
False
False
0
D
True
True
1
Attempts:
2 left
💡 Hint
Remember enums can be cast to their underlying integer values.
🧠 Conceptual
intermediate
1:30remaining
Choosing between enum and constants
Which scenario is best suited for using an enum instead of constants in C#?
AWhen you need a group of related named values with distinct integer values.
BWhen you want to define a single constant string value.
CWhen you want to define multiple unrelated constants scattered in code.
DWhen you want to store configuration settings that can change at runtime.
Attempts:
2 left
💡 Hint
Enums group related values under one type.
Predict Output
advanced
1: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);
    }
}
A5 (prints the integer value)
BBlue
CCompilation error
D5
Attempts:
2 left
💡 Hint
Casting an integer outside enum values is allowed but may produce unexpected output.
Predict Output
advanced
1: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);
    }
}
AHello World!
BHello, World!
CHello,World!
DHelloWorld!
Attempts:
2 left
💡 Hint
Look carefully at the commas and spaces in the concatenation.
🧠 Conceptual
expert
2:30remaining
Why prefer enums over constants for state management?
Why is using enums generally better than constants for managing states in C# applications?
AConstants can be changed at runtime, enums cannot.
BConstants use less memory than enums, so enums are slower.
CEnums provide type safety and better code readability compared to constants.
DEnums allow storing multiple unrelated values in one variable.
Attempts:
2 left
💡 Hint
Think about how enums help prevent invalid values.