0
0
Blockchain / Solidityprogramming~20 mins

Storage layout in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Storage Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Storage slot assignment in Solidity structs
Consider the following Solidity contract snippet. What will be the storage slot of the variable c inside the struct Data?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract StorageTest {
    struct Data {
        uint128 a;
        uint128 b;
        uint256 c;
    }

    Data public data;
}
ASlot 0
BSlot 2
CSlot 1
DSlot 3
Attempts:
2 left
💡 Hint
Remember that Solidity packs variables into 32-byte slots when possible.
Predict Output
intermediate
2:00remaining
Storage layout of dynamic arrays in Solidity
Given the following Solidity contract, what is the storage slot of the first element of the dynamic array arr?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract ArrayStorage {
    uint256[] public arr;
}
ASlot 1
Bkeccak256(0)
CSlot 0
Dkeccak256(1)
Attempts:
2 left
💡 Hint
Dynamic arrays store their length at the declared slot, elements start at keccak256(slot).
🔧 Debug
advanced
3:00remaining
Identify storage collision in upgradeable contracts
You have two versions of a Solidity contract. Version 1 has variables uint256 x and uint256 y. Version 2 adds a new variable uint256 z before y. What problem arises with this change?
AStorage collision causing <code>y</code> to overwrite <code>z</code>
BNo problem, variables are automatically reordered
CCompiler error due to variable reordering
DStorage collision causing <code>z</code> to overwrite <code>y</code>
Attempts:
2 left
💡 Hint
Changing variable order in upgradeable contracts can cause storage slot mismatches.
🧠 Conceptual
advanced
2:30remaining
Storage layout of mappings in Solidity
How does Solidity compute the storage slot for a value in a mapping like mapping(address => uint256) balances stored at slot 3?
Akeccak256(abi.encodePacked(key, 3))
BSlot 3 + key
Ckeccak256(abi.encode(3, key))
DSlot 3 * key
Attempts:
2 left
💡 Hint
Mapping storage uses keccak256 hash of the key concatenated with the slot.
Predict Output
expert
3:00remaining
Storage layout with inheritance and variable shadowing
Consider these Solidity contracts:
pragma solidity ^0.8.0;

contract A {
    uint256 public x = 1;
}

contract B is A {
    uint256 public x = 2;
}
What is the value of B.x() when called?
A2
B1
CCompiler error due to variable shadowing
D0
Attempts:
2 left
💡 Hint
Derived contracts can shadow base contract variables, but the derived variable is accessed.