You are on page 1of 4

Question 1: What is the problem with the following code (2/2)?

contract Storage {

uint data;

//Should update the `data` storage variable above

function set(uint data) external {

//data = data?

In the Storage contract, there is a state variable uint data. In the set function, you are
attempting to update the value of the state variable, but you've also defined a function
parameter with the same name, uint data. This creates ambiguity in the code, and
Solidity will prioritize the local function parameter over the state variable.
So, when you use data = data within the set function, it will update the function
parameter data with its own value, effectively doing nothing to the state variable data.
This is not what you intend.
To correctly update the state variable data with the value passed as an argument to the
set function, you should use the this keyword (or self in older versions of Solidity) to
reference the state variable explicitly. Here's the corrected code:

contract Storage {
uint data;
function set(uint _data) external {
data = _data;
}
}
By using _data as a local variable and referencing the state variable with data, you
avoid the naming conflict and update the state variable correctly.
Question 2: What are the 2 container types in Solidity?
- Arrays: Arrays are a collection of elements of the same data type, and they can be
either of fixed size or dynamic size.
+ Fixed-size arrays: You specify the size of the array when it is declared, and this size
cannot be changed during the execution of the program.
+ Dynamic arrays: These arrays don't have a fixed size and can grow or shrink during
program execution. They are typically implemented using a dynamic data structure
like a list or a vector.
- Mappings: Mappings are similar to associative arrays or dictionaries in other
programming languages. They allow you to create key-value pairs, where the keys can
be of any data type and the values can be of any data type.
Question 3: How to declare a mapping of address to booleans in Solidity?
mapping(address => bool) addressToBooleanMapping;
In this declaration, addressToBooleanMapping is the name of the mapping, address is
the key type, and bool is the value type. This mapping associates Ethereum addresses
with boolean values, where each address can be associated with either true or false.
You can use this mapping to store and retrieve boolean values associated with specific
Ethereum addresses in your Solidity smart contract. For example, you can set a
boolean value for an address like this:
addressToBooleanMapping[msg.sender] = true; // Set the boolean value for the
sender's address to true
And you can retrieve the boolean value for an address like this:
bool isTrue = addressToBooleanMapping[someAddress]; // Get the boolean value
associated with someAddress
Question 4: What are the 2 ways to define custom data structure in Solidity?
In Solidity, you can define custom data structures using two main approaches: structs
and enums.
Structs (Structures): Structs allow you to define custom composite data types that
group together multiple variables with different data types under a single name.
Structs are useful for creating more complex data structures to represent objects or
records in your smart contract.
Here's an example of defining a struct in Solidity:
struct Person {
string name;
uint age;
}
In this example, a Person struct is defined, which includes two variables: name (a
string) and age (an unsigned integer). You can then create instances of the Person
struct to store data about individuals.
Enums (Enumerations): Enums allow you to define a custom data type with a finite set
of named values. Enums are useful when you want to represent a variable that can
only have one of a few distinct values, such as status or state.
Here's an example of defining an enum in Solidity:
enum Status {
Active,
Inactive,
Suspended
}
In this example, an Status enum is defined with three named values: Active, Inactive,
and Suspended. You can use this enum to represent the status of an entity, and it
enforces that the value must be one of the specified options.
Question 5: When would you use a struct vs an enum?
Structs and enums serve different purposes in Solidity, and the choice between using
one or the other depends on the specific requirements of your smart contract and the
nature of the data you need to represent. Here's when you might use a struct vs. an
enum:
Structs:
Complex Data Structures: You would use a struct when you need to define complex
data structures that group together multiple variables with different data types. Structs
allow you to represent objects or records that contain multiple pieces of data. For
example, you might use a struct to represent a user profile with fields like name, age,
and address.
Storing Records: When you want to store records or objects in a structured way.
Structs are particularly useful for organizing and storing data efficiently within your
smart contract.
Custom Data Types: When you need to create a custom data type with multiple fields.
Structs are suitable for creating custom composite data types tailored to your specific
use case.
Enums:
Limited and Distinct Options: You would use an enum when you have a variable that
can only have one of a few distinct values, and you want to enforce that the value
must be one of those specific options. Enums are great for representing states, status,
or categorical variables that have a limited set of possible values.
Code Clarity: When you want to improve code clarity and readability by using
descriptive, named values instead of numeric or string constants. Enums provide
meaningful names for specific states or options.
Switching and Control Flow: In situations where you want to use an enum variable to
control the flow of your smart contract based on different states or options. Enums are
often used in conditional statements or switch cases to determine the behavior of the
contract based on the value of the enum.

You might also like