Challenge - 5 Problems
Storage vs Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Remember that assigning a memory array to a storage array copies the data.
✗ Incorrect
The function creates a memory array of length 3, assigns values, then copies it to the storage array 'numbers'. The storage array length becomes 3, so the function returns 3.
❓ Predict Output
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Modifying a memory copy does not affect storage.
✗ Incorrect
The 'temp' array is a memory copy of 'data'. Changing 'temp[0]' does not change 'data[0]' in storage, so 'data[0]' remains 10.
🧠 Conceptual
advanced2:00remaining
Gas cost difference between storage and memory
Which statement about gas costs in Solidity is correct?
Attempts:
2 left
💡 Hint
Consider the permanence of storage vs memory.
✗ Incorrect
Writing to storage is expensive because it changes the blockchain state permanently. Memory is temporary and cheaper to write to during function execution.
❓ Predict Output
advanced2: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]; } }
Attempts:
2 left
💡 Hint
Storage pointers modify the original storage data.
✗ Incorrect
The 'ref' variable is a storage pointer to 'arr'. Changing 'ref[0]' changes 'arr[0]' in storage, so 'getFirst()' returns 100.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about temporary data and gas costs.
✗ Incorrect
Using 'memory' creates a temporary copy that does not write to storage, saving gas. 'Storage' parameters refer to persistent blockchain data and are more expensive to modify.