Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Enum declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this enum usage?
Consider this C# code snippet. What will be printed when it runs?
C Sharp (C#)
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

class Program {
    static void Main() {
        Days today = Days.Wednesday;
        Console.WriteLine((int)today);
    }
}
A0
B2
C3
D4
Attempts:
2 left
💡 Hint
Remember enums start numbering from 0 by default.
❓ Predict Output
intermediate
2:00remaining
What is the output of this enum with custom values?
Look at this enum with assigned values. What does the program print?
C Sharp (C#)
enum Status { Pending = 1, Active = 5, Closed = 10 }

class Program {
    static void Main() {
        Status s = Status.Active;
        Console.WriteLine((int)s);
    }
}
A5
B10
C1
D0
Attempts:
2 left
💡 Hint
Check the assigned value for Active.
❓ Predict Output
advanced
2:00remaining
What error does this enum declaration cause?
What error will this code produce when compiled?
C Sharp (C#)
enum Colors { Red, Green = 5, Blue, Green = 10 }

class Program {
    static void Main() {}
}
ACompile-time error: Enum values must be unique
BRuntime error: Duplicate enum member
CNo error, compiles successfully
DCompile-time error: Duplicate enum member name
Attempts:
2 left
💡 Hint
Check if enum member names can repeat.
❓ Predict Output
advanced
2:00remaining
What is the output of this enum with flags attribute?
Given this code using [Flags], what will be printed?
C Sharp (C#)
[Flags]
enum Permissions {
    None = 0,
    Read = 1,
    Write = 2,
    Execute = 4
}

class Program {
    static void Main() {
        Permissions p = Permissions.Read | Permissions.Write;
        Console.WriteLine(p);
    }
}
ARead, Write
B3
CRead Write
D1, 2
Attempts:
2 left
💡 Hint
The [Flags] attribute changes how the enum prints combined values.
🧠 Conceptual
expert
3:00remaining
Which enum declaration is valid and assigns correct values?
Which of these enum declarations compiles without error and assigns the values as shown?
Aenum Levels { Low = 1, Medium = 2, High = 5, Critical = 5 } // Duplicate value 5
Benum Levels { Low = 1, Medium, High = 5, Critical } // Low=1, Medium=2, High=5, Critical=6
Cenum Levels { Low, Medium = 2, High, Critical } // Low=0, Medium=2, High=3, Critical=4
Denum Levels { Low = 1, Medium = 2, High, Critical = 2 } // Duplicate value 2
Attempts:
2 left
💡 Hint
Check how enum values increment when not explicitly assigned.

Practice

(1/5)
1. What is the purpose of an enum in C#?
easy
A. To group related named constants together
B. To create a new class type
C. To define a method
D. To declare a variable

Solution

  1. Step 1: Understand enum concept

    An enum groups related named constants to make code clearer.
  2. Step 2: Compare with other options

    Enums are not classes, methods, or variables but a special type for constants.
  3. Final Answer:

    To group related named constants together -> Option A
  4. Quick Check:

    Enum = group constants [OK]
Hint: Enums group named constants, not variables or methods [OK]
Common Mistakes:
  • Thinking enum is a class
  • Confusing enum with method or variable
  • Assuming enum holds variable data
2. Which of the following is the correct syntax to declare an enum named Days with members Monday and Tuesday?
easy
A. enum Days: Monday, Tuesday;
B. enum Days = { Monday, Tuesday };
C. enum Days { Monday, Tuesday };
D. enum Days (Monday, Tuesday);

Solution

  1. Step 1: Recall enum declaration syntax

    Enums use curly braces with members separated by commas, no equals or parentheses.
  2. Step 2: Check each option

    enum Days { Monday, Tuesday }; matches correct syntax: enum Days { Monday, Tuesday };.
  3. Final Answer:

    enum Days { Monday, Tuesday }; -> Option C
  4. Quick Check:

    Enum syntax uses braces and commas [OK]
Hint: Use braces and commas for enum members [OK]
Common Mistakes:
  • Using equals sign (=) after enum name
  • Using parentheses instead of braces
  • Using colon (:) instead of opening brace
3. What is the output of this code?
enum Color { Red, Green, Blue };
Console.WriteLine((int)Color.Green);
medium
A. 0
B. 1
C. 2
D. Green

Solution

  1. Step 1: Understand default enum values

    Enum members start at 0 by default: Red=0, Green=1, Blue=2.
  2. Step 2: Cast enum member to int

    Printing (int)Color.Green outputs its numeric value 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    Green = 1 by default [OK]
Hint: Enum members start at 0 unless assigned [OK]
Common Mistakes:
  • Assuming first member starts at 1
  • Printing enum name instead of int value
  • Confusing enum member order
4. Identify the error in this enum declaration:
enum Status
{
  Started = 1,
  InProgress = 2,
  Completed = "Done"
};
medium
A. Enum members must be separated by semicolons
B. Missing semicolon after enum declaration
C. Enum name must be lowercase
D. Enum members cannot have string values

Solution

  1. Step 1: Check enum member value types

    Enum members must be integral types (int, byte, etc.), not strings.
  2. Step 2: Analyze the given code

    "Completed = \"Done\"" assigns a string, which is invalid.
  3. Final Answer:

    Enum members cannot have string values -> Option D
  4. Quick Check:

    Enum values must be integers [OK]
Hint: Enum values must be integers, not strings [OK]
Common Mistakes:
  • Assigning string values to enum members
  • Thinking semicolon is required after enum
  • Assuming enum names must be lowercase
5. How can you declare an enum Priority with members Low=1, Medium=5, and High=10 so that the values are not sequential?
hard
A. enum Priority { Low = 1, Medium = 5, High = 10 };
B. enum Priority { Low, Medium, High }
C. enum Priority { Low = 1; Medium = 5; High = 10 }
D. enum Priority = { Low = 1, Medium = 5, High = 10 }

Solution

  1. Step 1: Assign explicit values to enum members

    To set custom values, assign each member an integer with = and commas between.
  2. Step 2: Check syntax correctness

    enum Priority { Low = 1, Medium = 5, High = 10 }; uses correct syntax with commas and explicit values.
  3. Final Answer:

    enum Priority { Low = 1, Medium = 5, High = 10 }; -> Option A
  4. Quick Check:

    Use = and commas for custom enum values [OK]
Hint: Assign values with = and separate members by commas [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Omitting explicit values for custom numbers
  • Using equals sign after enum name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard