0
0
Blockchain / Solidityprogramming~20 mins

Variable packing in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Variable Packing
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Solidity variable packing in storage
Consider the following Solidity contract snippet. What will be the value of slot0 after deployment?

contract Test {
    uint128 a = 1;
    uint128 b = 2;
    uint256 slot0;

    function readSlot0() public view returns (uint256) {
        assembly {
            slot0 := sload(0)
        }
    }
}
Blockchain / Solidity
contract Test {
    uint128 a = 1;
    uint128 b = 2;
    uint256 slot0;

    function readSlot0() public view returns (uint256) {
        assembly {
            slot0 := sload(0)
        }
    }
}
A0x0000000000000000000000000000000000000000000000000000000100000002
B0x0000000000000000000000000000000000000000000000000000000200000001
C0x0000000000000000000000000000000000000000000000000000000000000002
D0x0000000000000000000000000000000000000000000000000000000000000003
Attempts:
2 left
💡 Hint
Remember that two uint128 variables fit into one 256-bit storage slot, with the first declared variable occupying the lower bits.
🧠 Conceptual
intermediate
1:30remaining
Understanding variable packing order in Solidity
In Solidity, which of the following statements about variable packing in storage is correct?
AVariables are packed in the order they are declared, starting from the highest bits of the slot.
BVariables of different types cannot be packed together in the same storage slot.
CVariables are packed randomly in storage slots to optimize gas usage.
DVariables are packed in the order they are declared, starting from the lowest bits of the slot.
Attempts:
2 left
💡 Hint
Think about how Solidity stores smaller variables in a 256-bit slot.
Predict Output
advanced
2:30remaining
Output of packed struct storage in Solidity
Given the following Solidity contract, what is the value returned by readSlot0() after deployment?

contract Test {
    struct Data {
        uint64 x;
        uint64 y;
        uint128 z;
    }

    Data public data = Data(1, 2, 3);

    function readSlot0() public view returns (uint256) {
        assembly {
            let val := sload(0)
            mstore(0x0, val)
            return(0x0, 32)
        }
    }
}
Blockchain / Solidity
contract Test {
    struct Data {
        uint64 x;
        uint64 y;
        uint128 z;
    }

    Data public data = Data(1, 2, 3);

    function readSlot0() public view returns (uint256) {
        assembly {
            let val := sload(0)
            mstore(0x0, val)
            return(0x0, 32)
        }
    }
}
A0x000000000000000300000000000000020000000000000001
B0x000000000000000000000000000000030000000200000001
C0x00000000000000000000000000000000000000020000000103
D0x0000000000000000000000000000000000000000000000000000000000000006
Attempts:
2 left
💡 Hint
Remember the order and size of struct members and how they pack into a 256-bit slot.
🔧 Debug
advanced
2:00remaining
Identify the error in variable packing usage
A developer tries to pack variables in Solidity as follows:

contract Test {
    uint8 a = 1;
    uint256 b = 2;
    uint8 c = 3;
}

What is the main issue with this variable packing approach?
AVariables 'a' and 'c' cannot be packed because they are not declared consecutively without a larger type in between.
BVariables 'a' and 'c' will be packed together in the same slot, but 'b' will occupy a new slot, causing inefficient packing.
CThe compiler will throw a syntax error because uint8 and uint256 cannot be declared in the same contract.
DAll variables will be packed into a single 256-bit slot, causing overflow.
Attempts:
2 left
💡 Hint
Think about how Solidity packs variables and the effect of a large variable between smaller ones.
🚀 Application
expert
3:00remaining
Calculate total storage slots used by packed variables
Consider this Solidity contract:

contract Test {
    uint128 a;
    uint64 b;
    uint64 c;
    uint256 d;
    uint8 e;
    uint8 f;
    uint16 g;
}

How many 256-bit storage slots will these variables occupy in total?
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Group variables by their declaration order and sizes to see how they fit into 256-bit slots.