Consider the following Solidity contract snippet. What will be the output of the getBalance function call?
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]; } }
Think about which address is passed to getBalance and what the mapping contains.
The constructor sets the balance of the contract deployer (msg.sender) to 100. If you call getBalance with any other address, the mapping returns the default value 0.
Choose the correct statement about reading state variables in a smart contract.
Consider the difference between view functions and transactions.
View functions that only read state can be called externally without spending gas because they do not change the blockchain state.
Examine the following Solidity function that reads a contract's state. Why does calling getOwnerBalance revert?
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); } }
Check the require condition and what happens if resetOwner is called before getOwnerBalance.
The require statement checks if owner is not zero address. If resetOwner sets owner to zero, the require fails and the function reverts.
Given a public state variable uint public count;, which function correctly returns its value?
pragma solidity ^0.8.0;
contract Counter {
uint public count;
// Choose the correct function to read count
}Remember the keywords needed to read state variables without modifying state.
The function must be marked view to read state without modifying it. It must be at least public or external to be called from outside. pure disallows reading state.
Consider this Solidity contract that stores user addresses and balances. After calling getUsersWithBalanceAbove(50), how many addresses are returned?
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; } }
Count how many users have balances strictly greater than 50.
Users with balances 100 and 60 are above 50, so 2 addresses are returned.