0
0
Blockchain / Solidityprogramming~20 mins

Function modifiers in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple modifier in Solidity
What is the output of this Solidity contract when calling test() function?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract ModifierExample {
    string public message = "";

    modifier onlyOnce() {
        require(bytes(message).length == 0, "Already set");
        _;
    }

    function test() public onlyOnce {
        message = "Hello, blockchain!";
    }
}
AThe contract throws a runtime error on the first call to test().
B"Hello, blockchain!" is stored in message after calling test() once.
C"" (empty string) remains in message after calling test() once.
DThe contract throws a compile-time error due to modifier syntax.
Attempts:
2 left
💡 Hint
Think about what the modifier checks before allowing the function to run.
Predict Output
intermediate
2:00remaining
Effect of modifier order on function execution
Consider these two modifiers and a function using both. What is the output of calling execute()?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract ModifierOrder {
    string public log = "";

    modifier first() {
        log = string(abi.encodePacked(log, "1"));
        _;
    }

    modifier second() {
        log = string(abi.encodePacked(log, "2"));
        _;
    }

    function execute() public first second {
        log = string(abi.encodePacked(log, "E"));
    }
}
A"12E"
B"21E"
C"1E2"
D"2E1"
Attempts:
2 left
💡 Hint
Modifiers run in the order they are listed before the function body.
🔧 Debug
advanced
2:00remaining
Identify the error caused by modifier usage
What error occurs when calling withdraw() in this contract?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract WithdrawExample {
    address public owner;
    uint public balance = 100;

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function withdraw(uint amount) public onlyOwner {
        require(amount <= balance, "Insufficient balance");
        balance -= amount;
        payable(msg.sender).transfer(amount);
    }
}
ANo error; function executes successfully.
BCompile-time error: 'payable' keyword used incorrectly.
CRuntime error: 'require' fails because msg.sender is not owner.
DRuntime error: 'transfer' fails because contract has no Ether balance.
Attempts:
2 left
💡 Hint
Check if the contract has Ether to send before calling transfer.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in modifier definition
Which option correctly fixes the syntax error in this modifier?
Blockchain / Solidity
modifier checkValue(uint x) {
    require(x > 0);
    _;
}
AAdd a semicolon after require: require(x > 0); _;
BAdd a comma after require: require(x > 0), _;
CAdd curly braces around require: { require(x > 0) } _;
DAdd a colon after require: require(x > 0): _;
Attempts:
2 left
💡 Hint
Statements in Solidity must end with a semicolon.
🚀 Application
expert
3:00remaining
Using modifiers to restrict function access and update state
Given this contract, what is the value of counter after calling increment() twice from the owner address?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Counter {
    address public owner;
    uint public counter = 0;

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

    modifier incrementCounter() {
        _;
        counter += 1;
    }

    constructor() {
        owner = msg.sender;
    }

    function increment() public onlyOwner incrementCounter {
        // function body empty
    }
}
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
Consider the order modifiers run and how counter is updated.