Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly describes storage in Solidity?
easy
A. It stores data permanently on the blockchain.
B. It holds temporary data only during function execution.
C. It is used for read-only input data in external functions.
D. It is a type of event logging mechanism.

Solution

  1. Step 1: Understand storage role

    storage variables are saved permanently on the blockchain and persist between function calls.
  2. Step 2: Compare with memory and calldata

    memory is temporary during function execution, and calldata is read-only input data for external functions.
  3. Final Answer:

    It stores data permanently on the blockchain. -> Option A
  4. Quick Check:

    Storage = permanent data [OK]
Hint: Storage is permanent, memory is temporary, calldata is input [OK]
Common Mistakes:
  • Confusing storage with memory as temporary
  • Thinking calldata can be modified
  • Assuming storage is read-only
2. Which keyword is used to declare a function parameter that is read-only and passed from an external caller?
easy
A. calldata
B. storage
C. memory
D. event

Solution

  1. Step 1: Identify parameter data location for external input

    Function parameters from external callers that should not be modified use calldata.
  2. Step 2: Recall other data locations

    memory is for temporary data inside functions, storage is persistent, and event is unrelated to data location.
  3. Final Answer:

    calldata -> Option A
  4. Quick Check:

    External input = calldata [OK]
Hint: External read-only params use calldata [OK]
Common Mistakes:
  • Using memory for external parameters
  • Confusing event with data location
  • Using storage for function parameters
3. What will be the output of this Solidity function when called externally with _data = [1, 2, 3]?
function process(uint[] calldata _data) external pure returns (uint) {
    uint sum = 0;
    for (uint i = 0; i < _data.length; i++) {
        sum += _data[i];
    }
    return sum;
}
medium
A. Runtime error
B. 6
C. Compilation error
D. 0

Solution

  1. Step 1: Understand calldata usage

    The function receives an array _data as calldata, which is read-only but accessible for reading elements.
  2. Step 2: Calculate sum of array elements

    Sum of [1, 2, 3] is 1 + 2 + 3 = 6, which is returned.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Sum of input array = 6 [OK]
Hint: Sum array elements from calldata works like memory [OK]
Common Mistakes:
  • Thinking calldata cannot be read
  • Expecting compilation error due to calldata
  • Confusing sum calculation logic
4. Identify the error in this Solidity function:
function updateData(uint[] calldata _input) external {
    _input[0] = 10;
}
medium
A. Calldata arrays cannot be passed as parameters.
B. Function must be marked pure.
C. Missing return statement.
D. Cannot assign to calldata array elements.

Solution

  1. Step 1: Check mutability of calldata

    calldata is read-only, so assigning to _input[0] causes an error.
  2. Step 2: Validate other options

    The function can be non-pure, no return needed, and calldata arrays are allowed as parameters.
  3. Final Answer:

    Cannot assign to calldata array elements. -> Option D
  4. Quick Check:

    Calldata is read-only [OK]
Hint: Calldata arrays are read-only, no assignment allowed [OK]
Common Mistakes:
  • Trying to modify calldata elements
  • Thinking pure is required here
  • Assuming return is mandatory
5. You want to write a Solidity function that accepts a large array from an external caller, processes it temporarily, and then stores the result permanently. Which combination of data locations should you use for the parameter and the storage variable?
hard
A. Parameter as memory, storage variable as calldata
B. Parameter as storage, storage variable as memory
C. Parameter as calldata, storage variable as storage
D. Parameter as calldata, storage variable as memory

Solution

  1. Step 1: Choose parameter data location for large external input

    calldata is efficient and read-only for large external arrays, avoiding unnecessary copying.
  2. Step 2: Choose storage variable for permanent data

    To keep data permanently on blockchain, use storage for the variable that stores the processed result.
  3. Final Answer:

    Parameter as calldata, storage variable as storage -> Option C
  4. Quick Check:

    External input = calldata, permanent store = storage [OK]
Hint: Use calldata for input, storage for permanent save [OK]
Common Mistakes:
  • Using memory for large external input (inefficient)
  • Trying to store permanent data in memory or calldata
  • Confusing storage and memory roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard