Consider this Solidity contract snippet using an enum. What will be the output when getStatus() is called?
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"; } }
Look at the constructor and see which enum value is assigned to status.
The constructor sets status to Status.Shipped. The getStatus() function returns the string "Shipped" when the status matches Status.Shipped.
In Solidity, enums start with the first member assigned which integer value?
Think about how counting usually starts in programming.
In Solidity, enum members start counting from 0 by default. The first member has the value 0, the second 1, and so on.
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);
}Check the valid range of enum values and what happens if you pass a number outside that range.
Enum values in Solidity correspond to integers starting at 0. The enum Color has 3 members (0 to 2). Passing 3 is out of range and causes a runtime error.
Choose the correct Solidity code snippet that declares an enum and assigns a value to a state variable.
Remember how to refer to enum members with their enum type.
Option A correctly declares the enum and assigns the member using State.Active. Option A misses the enum type prefix. Option A uses invalid syntax with brackets. Option A assigns an integer directly without casting to the enum type, which is invalid in Solidity.
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;
}
}Look at how type(Light).max relates to enum members.
The enum Light has three members: Red (0), Yellow (1), Green (2). The maximum value is 2, so adding 1 gives the total count 3.