Consider this Solidity smart contract snippet:
pragma solidity ^0.8.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}If we call set(42) and then get(), what will get() return?
pragma solidity ^0.8.0; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }
Think about what the set function does before calling get.
The set function stores the value 42 in storedData. Then get returns that stored value, so the output is 42.
Choose the best description of a smart contract in blockchain technology.
Think about what makes smart contracts different from traditional contracts.
Smart contracts are programs that run on blockchains and automatically enforce rules without intermediaries.
Examine this Solidity function:
function divide(uint a, uint b) public pure returns (uint) {
return a / b;
}What happens if b is zero when calling divide(10, 0)?
function divide(uint a, uint b) public pure returns (uint) {
return a / b;
}What happens in programming when you divide by zero?
Dividing by zero causes a runtime error in Solidity, which reverts the transaction.
In Solidity, which function declaration allows the function to receive Ether?
Look for the correct keyword and syntax for payable functions.
The keyword payable must be used without parentheses to allow receiving Ether.
Given this Solidity contract snippet:
contract Registry {
mapping(address => bool) public registered;
function register() public {
registered[msg.sender] = true;
}
}If three different users call register() once each, how many entries will registered contain?
Think about how mappings store data for unique keys.
Each unique address key set to true counts as one entry. Three users mean three entries.