Consider this simple Solidity function that returns the sum of two numbers:
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}What will be the output when calling add(5, 7)?
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}Think about what adding two numbers means in Solidity.
The function adds two unsigned integers and returns their sum. Calling add(5, 7) returns 12.
Which of the following reasons best explains why Solidity is the main language used for Ethereum smart contracts?
Think about what makes a language suitable for Ethereum smart contracts.
Solidity is designed to work with the Ethereum Virtual Machine and supports contract-oriented programming, making it ideal for writing smart contracts on Ethereum.
Examine this Solidity code snippet:
contract Test {
uint public value;
function setValue(uint _value) public {
value = _value;
}
function getValue() public view returns (uint) {
return value;
}
function resetValue() public {
value = ;
}
}What error will the compiler show?
Look carefully at the assignment in resetValue.
The line value = ; is missing a value after the equals sign, causing a syntax error.
Given these two contracts:
contract A {
function greet() public pure returns (string memory) {
return "Hello from A";
}
}
contract B is A {
function greet() public pure returns (string memory) {
return "Hello from B";
}
}If you call greet() on contract B, what will be the output?
Think about how function overriding works in Solidity inheritance.
Contract B overrides the greet function from contract A, so calling greet() on B returns "Hello from B".
Consider this Solidity contract snippet:
contract Example {
mapping(address => uint) public balances;
function updateBalance(address user, uint amount) public {
if (amount > 100) {
balances[user] = amount;
} else {
balances[user] = 0;
}
}
}After calling updateBalance(0x1234..., 50) and then updateBalance(0x1234..., 150), what is the final value of balances[0x1234...]?
Look at how the function updates the balance based on the amount.
The first call sets the balance to 0 because 50 is not greater than 100. The second call sets it to 150 because 150 is greater than 100. So the final balance is 150.