0
0
Blockchain / Solidityprogramming~20 mins

Reading contract state in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Reading Contract State
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 contract state read?

Consider the following Solidity contract snippet. What will be the output of the getBalance function call?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Wallet {
    mapping(address => uint) private balances;

    constructor() {
        balances[msg.sender] = 100;
    }

    function getBalance(address user) public view returns (uint) {
        return balances[user];
    }
}
A0
BRevert error
CCompilation error
D100
Attempts:
2 left
💡 Hint

Think about which address is passed to getBalance and what the mapping contains.

🧠 Conceptual
intermediate
1:30remaining
Which statement about reading contract state is true?

Choose the correct statement about reading state variables in a smart contract.

AState variables cannot be read from outside the contract.
BState variables can be read without gas cost if called externally as a view function.
CReading state variables always costs gas regardless of call type.
DState variables are automatically public and readable by anyone.
Attempts:
2 left
💡 Hint

Consider the difference between view functions and transactions.

🔧 Debug
advanced
2:30remaining
Why does this contract state read revert?

Examine the following Solidity function that reads a contract's state. Why does calling getOwnerBalance revert?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Bank {
    address private owner;
    mapping(address => uint) private balances;

    constructor() {
        owner = msg.sender;
        balances[owner] = 500;
    }

    function getOwnerBalance() public view returns (uint) {
        require(owner != address(0), "Owner not set");
        return balances[owner];
    }

    function resetOwner() public {
        owner = address(0);
    }
}
ABecause constructor did not initialize balances, it reverts.
BBecause balances mapping is private, reading it causes revert.
CBecause owner was reset to address(0), the require fails causing revert.
DBecause getOwnerBalance is not marked view, it reverts.
Attempts:
2 left
💡 Hint

Check the require condition and what happens if resetOwner is called before getOwnerBalance.

📝 Syntax
advanced
1:30remaining
Which option correctly reads a public state variable in Solidity?

Given a public state variable uint public count;, which function correctly returns its value?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Counter {
    uint public count;

    // Choose the correct function to read count
}
Afunction getCount() public returns (uint) { return count; }
Bfunction getCount() external pure returns (uint) { return count; }
Cfunction getCount() private view returns (uint) { return count; }
Dfunction getCount() public view returns (uint) { return count; }
Attempts:
2 left
💡 Hint

Remember the keywords needed to read state variables without modifying state.

🚀 Application
expert
3:00remaining
How many items are in the returned array after reading contract state?

Consider this Solidity contract that stores user addresses and balances. After calling getUsersWithBalanceAbove(50), how many addresses are returned?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract UserBalances {
    address[] private users;
    mapping(address => uint) private balances;

    constructor() {
        users.push(0x1111111111111111111111111111111111111111);
        balances[0x1111111111111111111111111111111111111111] = 100;
        users.push(0x2222222222222222222222222222222222222222);
        balances[0x2222222222222222222222222222222222222222] = 40;
        users.push(0x3333333333333333333333333333333333333333);
        balances[0x3333333333333333333333333333333333333333] = 60;
    }

    function getUsersWithBalanceAbove(uint threshold) public view returns (address[] memory) {
        uint count = 0;
        for (uint i = 0; i < users.length; i++) {
            if (balances[users[i]] > threshold) {
                count++;
            }
        }

        address[] memory result = new address[](count);
        uint index = 0;
        for (uint i = 0; i < users.length; i++) {
            if (balances[users[i]] > threshold) {
                result[index] = users[i];
                index++;
            }
        }
        return result;
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Count how many users have balances strictly greater than 50.