Consider this Solidity contract snippet. What will be the output when getBalance() is called after deploying the contract?
contract Wallet {
uint private balance = 100;
function getBalance() public view returns (uint) {
return balance;
}
}Look at the initial value of balance and the return statement.
The function getBalance() returns the private variable balance which is initialized to 100. So the output is 100.
Which of the following best explains why functions define contract behavior in blockchain smart contracts?
Think about what functions do in programming and how contracts interact with users.
Functions in smart contracts define what actions the contract can perform and the rules it enforces. They are the core of contract behavior.
What error will occur when calling withdraw(uint amount) in this contract?
contract Bank {
uint public balance = 50;
function withdraw(uint amount) public returns (uint) {
if(amount <= balance) {
balance -= amount;
}
return balance;
}
}Check the function return type and the return statement.
The function withdraw is declared without a return type but tries to return balance. This causes a compilation error.
Choose the correct syntax for a function that can receive Ether payments.
Remember the keyword to allow a function to receive Ether and the required visibility.
The correct syntax requires the payable keyword and a visibility specifier like public. Option C is correct.
Given this contract, what is the value of count after calling increment() three times?
contract Counter {
uint public count = 0;
function increment() public {
count += 1;
}
}Each call to increment() adds 1 to count.
Starting from 0, calling increment() three times adds 1 each time, resulting in 3.