0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Storage vs Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity function?

Consider the following Solidity function that modifies an array passed as memory and storage. What will be the final value of myArray after calling modifyArrays?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public myArray = [1, 2, 3];

    function modifyArrays(uint[] memory memArray) public {
        memArray[0] = 100;
        myArray[1] = 200;
    }
}
A[100, 200, 3]
B[1, 2, 3]
C[100, 2, 3]
D[1, 200, 3]
Attempts:
2 left
💡 Hint

Remember that memory arrays are copies and changes to them do not affect storage arrays.

Predict Output
intermediate
2:00remaining
What error occurs when modifying calldata array?

What happens if you try to modify a calldata array inside a Solidity function?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    function modifyCalldata(uint[] calldata data) public pure {
        data[0] = 10;
    }
}
AFunction executes successfully, data[0] changed
BCompilation error: Cannot assign to calldata array element
CRuntime error: Out of gas
DWarning: Unused variable data
Attempts:
2 left
💡 Hint

Think about the mutability of calldata parameters.

🧠 Conceptual
advanced
2:00remaining
Why use calldata over memory for function parameters?

Which of the following is the main advantage of using calldata instead of memory for external function parameters in Solidity?

ACalldata is cheaper in gas because it avoids copying data
BCalldata variables can be resized dynamically
CCalldata variables persist after function execution
DCalldata allows modifying the input parameters
Attempts:
2 left
💡 Hint

Think about how data is passed and stored in Ethereum transactions.

Predict Output
advanced
2:00remaining
What is the output of this storage vs memory example?

What will be the output of the following Solidity code snippet?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public arr = [1, 2, 3];

    function test() public returns (uint) {
        uint[] storage ref = arr;
        uint[] memory copy = arr;
        ref[0] = 10;
        copy[1] = 20;
        return arr[0] + arr[1];
    }
}
A12
B3
C21
DCompilation error
Attempts:
2 left
💡 Hint

Remember that storage references affect the original array, but memory copies do not.

🧠 Conceptual
expert
2:00remaining
How does storage layout affect gas cost in Solidity?

Which statement best explains how storage layout impacts gas costs in Solidity smart contracts?

AStorage layout does not affect gas costs as all variables cost the same
BStorage variables are cheaper to access than memory variables
CAccessing and modifying <code>storage</code> variables is expensive because it involves writing to the blockchain state
DUsing <code>calldata</code> increases gas cost compared to storage
Attempts:
2 left
💡 Hint

Think about what happens when you change data stored on the blockchain.