0
0
Blockchain / Solidityprogramming~20 mins

Storage vs memory usage 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
Understanding storage and memory in Solidity
What will be the output of this Solidity function when called?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public numbers;

    function addNumbers() public returns (uint) {
        uint[] memory temp = new uint[](3);
        temp[0] = 1;
        temp[1] = 2;
        temp[2] = 3;

        numbers = temp;
        return numbers.length;
    }
}
A0
BRuntime error
CCompilation error
D3
Attempts:
2 left
💡 Hint
Remember that assigning a memory array to a storage array copies the data.
Predict Output
intermediate
2:00remaining
Effect of modifying memory vs storage variables
What will be the value of 'data[0]' after calling 'modify()'?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Example {
    uint[] public data;

    constructor() {
        data.push(10);
        data.push(20);
    }

    function modify() public {
        uint[] memory temp = data;
        temp[0] = 99;
    }
}
A10
B99
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Modifying a memory copy does not affect storage.
🧠 Conceptual
advanced
2:00remaining
Gas cost difference between storage and memory
Which statement about gas costs in Solidity is correct?
AReading from storage is cheaper than reading from memory.
BWriting to storage is more expensive than writing to memory.
CMemory usage permanently increases gas cost after function execution.
DStorage variables are temporary and cheaper to use than memory variables.
Attempts:
2 left
💡 Hint
Consider the permanence of storage vs memory.
Predict Output
advanced
2:00remaining
Storage pointer vs memory copy behavior
What will be the output of the function 'getFirst()' after calling 'update()'?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract PointerTest {
    uint[] public arr;

    constructor() {
        arr.push(5);
        arr.push(10);
    }

    function update() public {
        uint[] storage ref = arr;
        ref[0] = 100;
    }

    function getFirst() public view returns (uint) {
        return arr[0];
    }
}
A100
B0
C5
DCompilation error
Attempts:
2 left
💡 Hint
Storage pointers modify the original storage data.
🧠 Conceptual
expert
2:00remaining
Choosing storage vs memory for function parameters
Which is the best reason to use 'memory' instead of 'storage' for a function parameter in Solidity?
ATo allow the function to modify the original storage variable directly.
BTo permanently save changes to the blockchain state.
CTo reduce gas costs by avoiding expensive storage writes.
DTo make the variable accessible outside the function.
Attempts:
2 left
💡 Hint
Think about temporary data and gas costs.