Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Enums in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enum Mastery Badge
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 Solidity enum code?

Consider this Solidity contract snippet using an enum. What will be the output when getStatus() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Order {
    enum Status { Pending, Shipped, Delivered, Cancelled }
    Status public status;

    constructor() {
        status = Status.Shipped;
    }

    function getStatus() public view returns (string memory) {
        if (status == Status.Pending) return "Pending";
        else if (status == Status.Shipped) return "Shipped";
        else if (status == Status.Delivered) return "Delivered";
        else return "Cancelled";
    }
}
A"Shipped"
B"Pending"
C"Delivered"
D"Cancelled"
Attempts:
2 left
💡 Hint

Look at the constructor and see which enum value is assigned to status.

🧠 Conceptual
intermediate
1:00remaining
What integer value does the first enum member have in Solidity?

In Solidity, enums start with the first member assigned which integer value?

AUndefined until assigned
B1
C-1
D0
Attempts:
2 left
💡 Hint

Think about how counting usually starts in programming.

🔧 Debug
advanced
2:00remaining
Which option causes a runtime error in this Solidity enum usage?

Given this enum and function, which option will cause a runtime error?

enum Color { Red, Green, Blue }

function setColor(uint c) public pure returns (Color) {
    return Color(c);
}
ACalling setColor(1) returns Color.Green
BCalling setColor(0) returns Color.Red
CCalling setColor(3) causes an error
DCalling setColor(2) returns Color.Blue
Attempts:
2 left
💡 Hint

Check the valid range of enum values and what happens if you pass a number outside that range.

📝 Syntax
advanced
2:00remaining
Which option correctly declares and uses an enum in Solidity?

Choose the correct Solidity code snippet that declares an enum and assigns a value to a state variable.

A
enum State { Active, Inactive }
State public state = State.Active;
B
enum State { Active, Inactive }
State public state = Active;
C
enum State { Active, Inactive }
State state = State[0];
D
enum State { Active, Inactive }
State state = 1;
Attempts:
2 left
💡 Hint

Remember how to refer to enum members with their enum type.

🚀 Application
expert
3:00remaining
How many items are in the enum after this Solidity code runs?

Given this enum and contract, how many enum members exist?

pragma solidity ^0.8.0;

contract TrafficLight {
    enum Light { Red, Yellow, Green }

    function countLights() public pure returns (uint) {
        return uint(type(Light).max) + 1;
    }
}
A2
B3
C4
DCompilation error
Attempts:
2 left
💡 Hint

Look at how type(Light).max relates to enum members.

Practice

(1/5)
1. What is the main purpose of using enum in blockchain programming?
easy
A. To execute smart contracts faster
B. To create random numbers for cryptography
C. To store large amounts of data efficiently
D. To group related named values together for clarity and safety

Solution

  1. Step 1: Understand enum purpose

    Enums group related named values, making code easier to read and safer.
  2. Step 2: Compare options

    Only To group related named values together for clarity and safety correctly describes enums' main purpose in blockchain code.
  3. Final Answer:

    To group related named values together for clarity and safety -> Option D
  4. Quick Check:

    Enums group named values = A [OK]
Hint: Enums group fixed options with names, not data or speed [OK]
Common Mistakes:
  • Confusing enums with random number generators
  • Thinking enums store large data
  • Believing enums speed up contract execution
2. Which of the following is the correct syntax to declare an enum named Status with values Pending, Active, and Closed in Solidity?
easy
A. enum Status: Pending, Active, Closed
B. enum Status = (Pending, Active, Closed);
C. enum Status { Pending, Active, Closed }
D. enum Status { 'Pending', 'Active', 'Closed' }

Solution

  1. Step 1: Recall Solidity enum syntax

    In Solidity, enums are declared with enum Name { Value1, Value2, ... } without equals or colons.
  2. Step 2: Check each option

    enum Status { Pending, Active, Closed } matches correct syntax; others use invalid symbols or quotes.
  3. Final Answer:

    enum Status { Pending, Active, Closed } -> Option C
  4. Quick Check:

    Solidity enum syntax uses braces and commas = C [OK]
Hint: Enums use braces and commas, no equals or quotes [OK]
Common Mistakes:
  • Using equals sign instead of braces
  • Adding quotes around enum values
  • Using colons instead of braces
3. Given the Solidity enum declaration:
enum Role { User, Admin, Guest }

and the code:
Role userRole = Role.Admin;
if (userRole == Role.Guest) {
return "Guest Access";
} else {
return "Not Guest";
}

What will be the output?
medium
A. "Not Guest"
B. "Guest Access"
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand enum values and assignment

    Role.Admin is assigned to userRole, which is not equal to Role.Guest.
  2. Step 2: Evaluate the if condition

    The condition userRole == Role.Guest is false, so the else branch runs returning "Not Guest".
  3. Final Answer:

    "Not Guest" -> Option A
  4. Quick Check:

    Role.Admin != Role.Guest, so output = "Not Guest" [OK]
Hint: Compare enum values directly; Admin ≠ Guest means else runs [OK]
Common Mistakes:
  • Assuming Admin equals Guest
  • Expecting compilation or runtime errors
  • Confusing enum names with strings
4. Identify the error in this Solidity enum usage:
enum State { Created, Locked, Inactive }
State public state;
function setState(uint _state) public {
state = _state;
}
medium
A. Enum declaration syntax is wrong
B. Cannot assign uint directly to enum variable
C. Function setState should be private
D. Missing return type in function

Solution

  1. Step 1: Analyze enum assignment

    Enums require assignment from enum values, not raw uint integers.
  2. Step 2: Check function assignment

    Assigning _state (uint) directly to state (enum) causes a type mismatch error.
  3. Final Answer:

    Cannot assign uint directly to enum variable -> Option B
  4. Quick Check:

    Enum variables need enum values, not uints [OK]
Hint: Assign enum values, not integers, to enum variables [OK]
Common Mistakes:
  • Assigning integers directly to enums
  • Thinking enum syntax is wrong
  • Confusing function visibility with errors
5. You want to represent user roles in a blockchain contract using enums. You need to check if a user has either Admin or Moderator role to allow special access. Given:
enum Role { User, Admin, Moderator, Guest }

Which of the following Solidity code snippets correctly checks for this condition?
hard
A. if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ }
B. if (userRole == Role.Admin && userRole == Role.Moderator) { /* allow access */ }
C. if (userRole in [Role.Admin, Role.Moderator]) { /* allow access */ }
D. if (userRole.equals(Role.Admin) || userRole.equals(Role.Moderator)) { /* allow access */ }

Solution

  1. Step 1: Understand logical conditions for enums

    To allow access if userRole is Admin OR Moderator, use logical OR (||) between equality checks.
  2. Step 2: Evaluate each option

    if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } uses correct OR condition; B uses AND which is impossible; C uses invalid syntax; A uses non-existent method equals.
  3. Final Answer:

    if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } -> Option A
  4. Quick Check:

    Use || for OR between enum comparisons [OK]
Hint: Use || to check multiple enum values, not && or invalid syntax [OK]
Common Mistakes:
  • Using && instead of || for OR condition
  • Trying to use 'in' keyword for enums
  • Calling non-existent methods on enums
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