0
0
Blockchain / Solidityprogramming~20 mins

Why logic controls execution in Blockchain / Solidity - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logic Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this smart contract function?

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?

Blockchain / Solidity
function transfer(uint amount) public returns (string memory) {
    if (amount > 100) {
        return "Transfer denied: amount too large";
    } else {
        return "Transfer successful";
    }
}
ARuntime error
B"Transfer successful"
CCompilation error
D"Transfer denied: amount too large"
Attempts:
2 left
💡 Hint

Think about what the if condition checks and what happens when the amount is 150.

Predict Output
intermediate
2:00remaining
What does this Solidity code output?

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)?

Blockchain / Solidity
function calculateFee(uint amount) public pure returns (uint) {
    if (amount <= 50) {
        return 1;
    } else if (amount <= 100) {
        return 2;
    } else {
        return 5;
    }
}
A2
B5
C1
D0
Attempts:
2 left
💡 Hint

Check which condition 75 satisfies.

🔧 Debug
advanced
2:00remaining
Why does this smart contract function always revert?

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?

Blockchain / Solidity
function approve(uint amount) public returns (bool) {
    require(amount < 0, "Amount must be positive");
    return true;
}
ABecause amount can never be less than 0 since it's unsigned, so require always fails
BBecause the function should use if instead of require
CBecause the function is missing a return statement
DBecause require needs a semicolon after the message string
Attempts:
2 left
💡 Hint

Think about the type of amount and what the condition checks.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this Solidity function?

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?

Blockchain / Solidity
function checkOwner(address user) public view returns (bool) {
    if user == owner {
        return true;
    } else {
        return false;
    }
}
Aif (user == owner) { return true; } else { return false; }
Breturn user == owner;
Cif user == owner { return true; } else { return false; }
Dif (user == owner) return true; else return false;
Attempts:
2 left
💡 Hint

Remember how conditions are written in Solidity.

🚀 Application
expert
3:00remaining
How many times will the event be emitted in this Solidity loop?

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?

Blockchain / Solidity
event LogNumber(uint number);

function emitEvenNumbers() public {
    for (uint i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            emit LogNumber(i);
        }
    }
}
A4
B3
C2
D5
Attempts:
2 left
💡 Hint

Count how many numbers from 0 to 4 are even.