0
0
Blockchain / Solidityprogramming~20 mins

Mappings in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity mapping access?

Consider the following Solidity code snippet:

pragma solidity ^0.8.0;

contract Test {
    mapping(address => uint) public balances;

    function setBalance(address user, uint amount) public {
        balances[user] = amount;
    }

    function getBalance(address user) public view returns (uint) {
        return balances[user];
    }
}

If no balance has been set for address 0x1234..., what will getBalance(0x1234...) return?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    mapping(address => uint) public balances;

    function setBalance(address user, uint amount) public {
        balances[user] = amount;
    }

    function getBalance(address user) public view returns (uint) {
        return balances[user];
    }
}
AReturns a random value
BThrows an error because the key does not exist
C0
DReturns the maximum uint value
Attempts:
2 left
💡 Hint

Think about what default values Solidity returns for mappings when a key is not set.

🧠 Conceptual
intermediate
1:30remaining
Why can't you iterate over a Solidity mapping?

Mappings in Solidity do not support iteration. Why is this the case?

ABecause mappings do not store keys, only values
BBecause Solidity does not allow loops inside contracts
CBecause mappings are only temporary and get deleted after function execution
DBecause mappings are stored in a way that does not keep track of keys, making iteration impossible
Attempts:
2 left
💡 Hint

Think about how mappings are stored in the Ethereum storage.

🔧 Debug
advanced
2:00remaining
What error does this Solidity code raise?

Examine this Solidity code snippet:

pragma solidity ^0.8.0;

contract Example {
    mapping(uint => string) public data;

    function addData(uint key, string memory value) public {
        data[key] = value;
    }

    function getData(uint key) public view returns (string memory) {
        return data[key];
    }

    function deleteData(uint key) public {
        delete data[key];
    }

    function test() public {
        string memory val = data[1];
        val = "new value";
    }
}

What error will occur when compiling or running this contract?

ANo error, the code compiles and runs fine
BTypeError: Cannot assign to a memory variable that is not a storage pointer
CCompilation error: Mappings cannot have string as value type
DRuntime error: Cannot delete mapping elements
Attempts:
2 left
💡 Hint

Check if the operations on mapping and string memory variables are valid.

Predict Output
advanced
1:30remaining
What is the value of the mapping after these operations?

Given this Solidity contract snippet:

pragma solidity ^0.8.0;

contract Counter {
    mapping(address => uint) public counts;

    function increment() public {
        counts[msg.sender]++;
    }

    function reset(address user) public {
        counts[user] = 0;
    }
}

If msg.sender calls increment() three times, then reset(msg.sender) once, what is the value of counts[msg.sender]?

A3
B0
C1
DUndefined
Attempts:
2 left
💡 Hint

Think about what the reset function does to the mapping value.

📝 Syntax
expert
2:00remaining
Which option will cause a compilation error in this mapping declaration?

Consider these Solidity mapping declarations. Which one will cause a compilation error?

Amapping(uint => function()) public callbacks;
Bmapping(address => uint) public balances;
Cmapping(bytes32 => uint) public nameToAge;
Dmapping(uint => mapping(address => bool)) public approvals;
Attempts:
2 left
💡 Hint

Think about what types are allowed as mapping values in Solidity.