0
0
Blockchain / Solidityprogramming~10 mins

Why data location affects cost in Blockchain / Solidity - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the data storage location in a smart contract.

Blockchain / Solidity
contract Storage {
    string public data;
    function setData(string memory [1]) public {
        data = [1];
    }
}
Drag options to blanks, or click blank then click option'
Ainput
Bvalue
Clocation
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'location' which don't clarify the data being stored.
2fill in blank
medium

Complete the code to calculate gas cost based on data location.

Blockchain / Solidity
function calculateCost(uint256 [1]) public pure returns (uint256) {
    uint256 cost = [1] * 2000;
    return cost;
}
Drag options to blanks, or click blank then click option'
Aprice
Blocation
Csize
Damount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'location' as a variable for size, which is confusing.
3fill in blank
hard

Fix the error in the code that calculates cost for storing data in different locations.

Blockchain / Solidity
function getCost(string memory [1]) public pure returns (uint256) {
    if (keccak256(bytes([1])) == keccak256(bytes("storage"))) {
        return 5000;
    } else {
        return 1000;
    }
}
Drag options to blanks, or click blank then click option'
AdataLocation
BlocationData
Cdata
Dloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match the variable inside the function.
4fill in blank
hard

Fill both blanks to create a dictionary that maps data locations to their gas costs.

Blockchain / Solidity
mapping(string => uint256) public gasCosts = {
    "storage": [1],
    "memory": [2]
};
Drag options to blanks, or click blank then click option'
A5000
B1000
C2000
D3000
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the gas costs between storage and memory.
5fill in blank
hard

Fill all three blanks to complete the function that returns gas cost based on data location and size.

Blockchain / Solidity
function computeGasCost(string memory [1], uint256 [2]) public pure returns (uint256) {
    uint256 baseCost = (keccak256(bytes([1])) == keccak256(bytes("storage"))) ? [3] : 1000;
    return baseCost * [2];
}
Drag options to blanks, or click blank then click option'
AdataLocation
BdataSize
C5000
D3000
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or incorrect gas cost values.