0
0
Blockchain / Solidityprogramming~20 mins

Gas usage testing in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gas Usage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Gas usage of simple storage update
Consider a smart contract function that updates a stored number. What is the gas usage behavior when calling this function multiple times with the same value?
Blockchain / Solidity
contract SimpleStorage {
    uint256 public storedData;

    function set(uint256 x) public {
        storedData = x;
    }
}
AGas cost is zero for calls with the same value as the current storedData.
BGas cost is the same for every call regardless of the value.
CGas cost is high only on the first update; subsequent calls with the same value cost less gas.
DGas cost increases with each call regardless of the value.
Attempts:
2 left
💡 Hint
Think about how Ethereum refunds gas when storage is not changed.
Predict Output
intermediate
2:00remaining
Gas cost of adding vs removing elements in a mapping
Given a mapping in a smart contract, what happens to gas usage when you add a new key-value pair versus when you delete a key?
Blockchain / Solidity
contract MapExample {
    mapping(address => uint256) public balances;

    function addBalance(address user, uint256 amount) public {
        balances[user] = amount;
    }

    function removeBalance(address user) public {
        delete balances[user];
    }
}
ADeleting a key costs more gas than adding one.
BAdding and deleting keys cost the same gas with no refunds.
CNeither adding nor deleting keys affects gas usage.
DAdding a new key costs more gas; deleting a key refunds some gas.
Attempts:
2 left
💡 Hint
Consider how storage refunds work in Ethereum.
🔧 Debug
advanced
3:00remaining
Unexpected high gas usage in loop
This contract function loops over an array and updates a mapping. Why does it consume unexpectedly high gas?
Blockchain / Solidity
contract LoopGas {
    mapping(uint256 => uint256) public data;
    uint256[] public keys;

    function updateData() public {
        for (uint i = 0; i < keys.length; i++) {
            data[keys[i]] = i;
        }
    }
}
AThe mapping update inside the loop is free and does not affect gas.
BThe loop condition uses <= which causes an out-of-bounds access and wastes gas.
CThe keys array is empty, so the loop does not run and gas is wasted.
DThe function is missing a payable modifier causing high gas.
Attempts:
2 left
💡 Hint
Check the loop boundary carefully.
📝 Syntax
advanced
2:00remaining
Gas optimization with unchecked arithmetic
Which option correctly uses unchecked arithmetic to save gas in Solidity 0.8+ without causing overflow errors?
Blockchain / Solidity
function sum(uint256 a, uint256 b) public pure returns (uint256) {
    // Fill in the blank
}
Aunchecked { return a + b; }
Breturn a + b; // no unchecked block
Cunchecked return a + b;
Dreturn unchecked(a + b);
Attempts:
2 left
💡 Hint
The unchecked block disables overflow checks inside it.
🚀 Application
expert
3:00remaining
Estimating gas cost for dynamic array resizing
You have a function that appends elements to a dynamic array in a smart contract. How does the gas cost behave as the array grows, and which option best describes this behavior?
Blockchain / Solidity
contract DynamicArray {
    uint256[] public numbers;

    function addNumber(uint256 num) public {
        numbers.push(num);
    }
}
AGas cost per push remains roughly constant regardless of array size.
BGas cost per push increases linearly as the array grows.
CGas cost per push decreases as the array grows due to storage optimization.
DGas cost per push is zero after the first element is added.
Attempts:
2 left
💡 Hint
Think about how storage slots are allocated for dynamic arrays.