0
0
Blockchain / Solidityprogramming~10 mins

Storage vs memory vs calldata in Blockchain / Solidity - Interactive Practice

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

Complete the code to declare a function parameter stored in memory.

Blockchain / Solidity
function greet(string [1] name) public pure returns (string memory) {
    return name;
}
Drag options to blanks, or click blank then click option'
Astorage
Bcalldata
Cvar
Dmemory
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'storage' for function parameters causes errors.
Using 'var' is not valid in Solidity.
2fill in blank
medium

Complete the code to declare a function parameter that is read-only and cheaper to use.

Blockchain / Solidity
function readData(string [1] data) external view returns (string memory) {
    return data;
}
Drag options to blanks, or click blank then click option'
Astorage
Bcalldata
Cmemory
Dconstant
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'memory' instead of 'calldata' increases gas cost.
Trying to modify calldata variables causes errors.
3fill in blank
hard

Fix the error in the code by choosing the correct data location for the state variable.

Blockchain / Solidity
contract Example {
    string [1] name;

    function setName(string memory newName) public {
        name = newName;
    }
}
Drag options to blanks, or click blank then click option'
Acalldata
Bmemory
Cstorage
Dconstant
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring state variables as 'memory' causes compilation errors.
Using 'calldata' for state variables is invalid.
4fill in blank
hard

Complete the code to create a function that accepts a read-only array and stores it in state.

Blockchain / Solidity
contract DataStore {
    uint[] public data;

    function storeData(uint[] [1] input) public {
        data = input;
    }
}
Drag options to blanks, or click blank then click option'
Acalldata
Bmemory
C.slice()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call .slice() on calldata arrays causes errors.
Using memory instead of calldata increases gas cost.
5fill in blank
hard

Fill both blanks to declare a function that modifies a storage array using a memory copy.

Blockchain / Solidity
contract Modify {
    uint[] public numbers;

    function updateNumbers(uint[] [1] newNumbers) public {
        uint[] storage nums = numbers;
        nums = newNumbers;
        nums.push([2]);
    }
}
Drag options to blanks, or click blank then click option'
Amemory
C100
Dcalldata
Attempts:
3 left
💡 Hint
Common Mistakes
Using calldata prevents modification.
Trying to assign calldata array directly to storage without copying.