An enum is a special type that lets you define a set of named values. It helps you group related options together, like days of the week or transaction states.
Click to reveal answer
beginner
Why use enums instead of plain numbers or strings?
Enums make code easier to read and less error-prone because you use meaningful names instead of numbers or strings. This helps prevent mistakes and makes your code clearer.
Click to reveal answer
beginner
How do you define an enum for transaction status with values: Pending, Completed, Failed?
Can enums have underlying numeric values in blockchain languages?
Yes, enums often have underlying numbers starting from zero by default. You can also assign specific numbers if needed, which helps when storing or comparing values.
Click to reveal answer
intermediate
How do enums improve smart contract security?
Enums limit possible values to a fixed set, reducing bugs from invalid inputs. This helps smart contracts behave predictably and safely.
Click to reveal answer
What does an enum represent in blockchain programming?
AA function to send transactions
BA variable that changes randomly
CA fixed set of named values
DA type of cryptocurrency
✗ Incorrect
Enums define a fixed set of named values to represent related options clearly.
Which of these is a correct enum definition for colors?
Aenum Colors { Red, Green, Blue }
Benum Colors = (Red, Green, Blue)
Cenum Colors: Red, Green, Blue
Denum Colors -> Red, Green, Blue
✗ Incorrect
The correct syntax uses curly braces and commas to list enum values.
What is the default underlying value of the first enum member?
A0
B1
CUndefined
DDepends on the blockchain
✗ Incorrect
By default, the first enum member has the numeric value 0.
How do enums help prevent bugs in smart contracts?
ABy speeding up transactions
BBy allowing any value to be used
CBy encrypting data automatically
DBy limiting values to a fixed set
✗ Incorrect
Enums restrict values to a fixed set, reducing invalid inputs and bugs.
Can you assign specific numbers to enum members?
ANo, enums only use default numbering
BYes, you can assign specific numeric values
COnly strings can be assigned
DOnly in off-chain code
✗ Incorrect
You can assign specific numbers to enum members if you want custom values.
Explain what an enum is and why it is useful in blockchain programming.
Think about how enums help organize options like transaction states.
You got /4 concepts.
Describe how enums can improve the security and reliability of smart contracts.
Consider how limiting choices helps avoid mistakes.
You got /4 concepts.
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
Step 1: Understand enum purpose
Enums group related named values, making code easier to read and safer.
Step 2: Compare options
Only To group related named values together for clarity and safety correctly describes enums' main purpose in blockchain code.
Final Answer:
To group related named values together for clarity and safety -> Option D
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
Step 1: Recall Solidity enum syntax
In Solidity, enums are declared with enum Name { Value1, Value2, ... } without equals or colons.
Step 2: Check each option
enum Status { Pending, Active, Closed } matches correct syntax; others use invalid symbols or quotes.
Final Answer:
enum Status { Pending, Active, Closed } -> Option C
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
Step 1: Understand enum values and assignment
Role.Admin is assigned to userRole, which is not equal to Role.Guest.
Step 2: Evaluate the if condition
The condition userRole == Role.Guest is false, so the else branch runs returning "Not Guest".
Final Answer:
"Not Guest" -> Option A
Quick Check:
Role.Admin != Role.Guest, so output = "Not Guest" [OK]
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
Step 1: Analyze enum assignment
Enums require assignment from enum values, not raw uint integers.
Step 2: Check function assignment
Assigning _state (uint) directly to state (enum) causes a type mismatch error.
Final Answer:
Cannot assign uint directly to enum variable -> Option B
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
Step 1: Understand logical conditions for enums
To allow access if userRole is Admin OR Moderator, use logical OR (||) between equality checks.
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.
Final Answer:
if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.