Consider this simplified smart contract function that controls token transfer based on a condition.
function transfer(uint amount) public returns (string memory) {
if (amount > 100) {
return "Transfer denied: amount too large";
} else {
return "Transfer successful";
}
}What will be the output when transfer(150) is called?
function transfer(uint amount) public returns (string memory) {
if (amount > 100) {
return "Transfer denied: amount too large";
} else {
return "Transfer successful";
}
}Think about what the if condition checks and what happens when the amount is 150.
The function checks if the amount is greater than 100. Since 150 > 100, it returns "Transfer denied: amount too large".
Look at this Solidity function that uses logic to decide the fee.
function calculateFee(uint amount) public pure returns (uint) {
if (amount <= 50) {
return 1;
} else if (amount <= 100) {
return 2;
} else {
return 5;
}
}What is the output of calculateFee(75)?
function calculateFee(uint amount) public pure returns (uint) {
if (amount <= 50) {
return 1;
} else if (amount <= 100) {
return 2;
} else {
return 5;
}
}Check which condition 75 satisfies.
75 is greater than 50 but less than or equal to 100, so the function returns 2.
Here is a Solidity function meant to approve spending only if the amount is positive.
function approve(uint amount) public returns (bool) {
require(amount < 0, "Amount must be positive");
return true;
}Why does this function always revert?
function approve(uint amount) public returns (bool) { require(amount < 0, "Amount must be positive"); return true; }
Think about the type of amount and what the condition checks.
In Solidity, uint is unsigned and cannot be negative. The condition amount < 0 is always false, so require always reverts.
Look at this function that uses logic to check ownership.
function checkOwner(address user) public view returns (bool) {
if user == owner {
return true;
} else {
return false;
}
}Which option shows the code with a syntax error?
function checkOwner(address user) public view returns (bool) { if user == owner { return true; } else { return false; } }
Remember how conditions are written in Solidity.
In Solidity, the condition in an if statement must be inside parentheses. Option C misses parentheses, causing a syntax error.
Consider this Solidity function that emits an event based on a logic condition inside a loop.
event LogNumber(uint number);
function emitEvenNumbers() public {
for (uint i = 0; i < 5; i++) {
if (i % 2 == 0) {
emit LogNumber(i);
}
}
}How many times will LogNumber be emitted when emitEvenNumbers() is called?
event LogNumber(uint number);
function emitEvenNumbers() public {
for (uint i = 0; i < 5; i++) {
if (i % 2 == 0) {
emit LogNumber(i);
}
}
}Count how many numbers from 0 to 4 are even.
The loop runs with i = 0,1,2,3,4. Even numbers are 0, 2, and 4, so the event emits 3 times.