0
0
Blockchain / Solidityprogramming~20 mins

Memory allocation in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Memory usage in Solidity storage vs memory

Consider the following Solidity function that copies an array from storage to memory and modifies it. What will be the value of arr[0] in storage after calling modifyArray()?

Blockchain / Solidity
pragma solidity ^0.8.0;

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

    function modifyArray() public {
        uint[] memory temp = arr;
        temp[0] = 100;
    }
}
A100
B1
C0
DCompilation error
Attempts:
2 left
💡 Hint

Remember that memory variables are copies and do not affect storage unless explicitly written back.

🧠 Conceptual
intermediate
1:30remaining
Understanding stack vs heap in smart contracts

Which statement correctly describes the difference between stack and heap memory in blockchain smart contracts?

AStack and heap memory are the same in smart contracts.
BStack memory is dynamic and used for large data, heap is fixed size and used for small variables.
CHeap memory is faster than stack memory and used for temporary variables.
DStack memory stores local variables and function calls, heap memory stores dynamically allocated data like arrays and structs.
Attempts:
2 left
💡 Hint

Think about where local variables and dynamic data are stored during execution.

🔧 Debug
advanced
2:00remaining
Detecting memory allocation error in Solidity

What error will the following Solidity code produce when compiled?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Demo {
    function test() public pure returns (uint) {
        uint[] memory arr;
        arr[0] = 10;
        return arr[0];
    }
}
ARuntime error: Out of bounds memory access
BReturns 10
CCompilation error: Memory array must have fixed length
DCompilation error: Missing return statement
Attempts:
2 left
💡 Hint

Think about how memory arrays must be declared in Solidity.

Predict Output
advanced
2:30remaining
Gas cost impact of storage vs memory

What will be the gas cost difference when running the following two functions in Solidity?

Function storeData() writes to storage, useMemory() uses memory only.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract GasTest {
    uint[] public data;

    function storeData() public {
        for (uint i = 0; i < 5; i++) {
            data.push(i);
        }
    }

    function useMemory() public pure returns (uint) {
        uint[] memory temp = new uint[](5);
        for (uint i = 0; i < 5; i++) {
            temp[i] = i;
        }
        return temp[4];
    }
}
AstoreData() costs more gas because storage writes are expensive; useMemory() costs less as memory is cheaper
BBoth functions cost the same gas because they do similar loops
CuseMemory() costs more gas because memory allocation is expensive
DstoreData() costs less gas because storage is persistent
Attempts:
2 left
💡 Hint

Recall that writing to blockchain storage is more costly than using temporary memory.

🧠 Conceptual
expert
3:00remaining
Memory allocation and reentrancy vulnerability

How can improper memory allocation contribute to a reentrancy attack vulnerability in a smart contract?

AMemory allocation has no impact on reentrancy since it is only temporary and isolated per call.
BImproper memory allocation causes out-of-gas errors, which prevent reentrancy.
CReentrancy attacks happen only due to storage variables, memory allocation is irrelevant.
DIf memory variables are not cleared properly, attackers can reuse them to manipulate contract state during reentrant calls.
Attempts:
2 left
💡 Hint

Memory is temporary and isolated per execution context in smart contracts.