Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a function modifier in blockchain smart contracts?
easy
A. To change the return type of a function
B. To add extra rules or checks around a function
C. To rename a function
D. To delete a function from the contract

Solution

  1. Step 1: Understand function modifiers

    Function modifiers are used to add extra rules or steps before or after a function runs.
  2. Step 2: Compare options

    Only To add extra rules or checks around a function correctly describes adding extra rules or checks around a function.
  3. Final Answer:

    To add extra rules or checks around a function -> Option B
  4. Quick Check:

    Function modifiers add rules [OK]
Hint: Modifiers add rules or checks around functions [OK]
Common Mistakes:
  • Thinking modifiers rename or delete functions
  • Confusing modifiers with return type changes
  • Believing modifiers execute functions themselves
2. Which of the following is the correct syntax to define a function modifier in Solidity?
easy
A. modifier onlyOwner() => { require(msg.sender == owner); _; }
B. modifier onlyOwner() : { require(msg.sender == owner); _; }
C. modifier onlyOwner() -> { require(msg.sender == owner); _; }
D. modifier onlyOwner() { require(msg.sender == owner); _; }

Solution

  1. Step 1: Recall Solidity modifier syntax

    Modifiers use the keyword 'modifier', name, parentheses, and curly braces with '_;' inside.
  2. Step 2: Identify correct syntax

    modifier onlyOwner() { require(msg.sender == owner); _; } matches the correct Solidity syntax with braces and '_;'. Others use invalid symbols like '=>', '->', or ':'.
  3. Final Answer:

    modifier onlyOwner() { require(msg.sender == owner); _; } -> Option D
  4. Quick Check:

    Modifier syntax uses braces and '_;' [OK]
Hint: Modifiers use braces { } and _; inside [OK]
Common Mistakes:
  • Using arrows (=>, ->) instead of braces
  • Omitting the '_;' statement
  • Using colon ':' instead of braces
3. What will be the output of the following Solidity code snippet?
contract Test {
    address owner;
    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }
    function setOwner(address _owner) public {
        owner = _owner;
    }
    function secureAction() public onlyOwner {
        // action code
    }
}
If msg.sender is not the owner and calls secureAction(), what happens?
medium
A. The function runs normally
B. The owner address changes
C. The transaction reverts with error 'Not owner'
D. The function ignores the modifier

Solution

  1. Step 1: Understand the modifier behavior

    The modifier checks if msg.sender equals owner; if not, it calls require which reverts with "Not owner".
  2. Step 2: Analyze the call with wrong sender

    If msg.sender is not owner, require fails and the transaction reverts with the error message.
  3. Final Answer:

    The transaction reverts with error 'Not owner' -> Option C
  4. Quick Check:

    require fails if sender not owner [OK]
Hint: require reverts if condition false inside modifier [OK]
Common Mistakes:
  • Assuming function runs even if require fails
  • Thinking owner changes on secureAction call
  • Ignoring the require statement effect
4. Identify the error in this Solidity code using a function modifier:
contract Sample {
    address owner;
    modifier onlyOwner() {
        require(msg.sender == owner);
        _
    }
    function changeOwner(address newOwner) public onlyOwner {
        owner = newOwner;
    }
}
medium
A. Missing semicolon after '_' in modifier
B. Incorrect function visibility for changeOwner
C. owner variable not initialized
D. Modifier name should be 'OnlyOwner' with capital O

Solution

  1. Step 1: Check modifier syntax

    The modifier has '_', but it misses the semicolon '_;' which is required to mark function execution point.
  2. Step 2: Verify other parts

    Function visibility 'public' is correct, owner can be uninitialized but not a syntax error, and modifier names are case-sensitive but not required to be capitalized.
  3. Final Answer:

    Missing semicolon after '_' in modifier -> Option A
  4. Quick Check:

    Modifier must have '_;' not just '_' [OK]
Hint: Modifier must end '_' with semicolon ';' [OK]
Common Mistakes:
  • Forgetting semicolon after '_' in modifier
  • Thinking function visibility causes error here
  • Assuming variable initialization causes syntax error
5. You want to create a modifier that allows a function to run only if the contract is not paused. The contract has a boolean paused. Which modifier code correctly implements this logic?
hard
A. modifier whenNotPaused() { require(!paused, "Paused"); _; }
B. modifier whenNotPaused() { if(paused) { revert("Paused"); } }
C. modifier whenNotPaused() { require(paused, "Paused"); _; }
D. modifier whenNotPaused() { if(!paused) { _; } }

Solution

  1. Step 1: Understand the paused logic

    The function should run only if paused is false, so require(!paused) ensures this.
  2. Step 2: Check modifier completeness

    modifier whenNotPaused() { require(!paused, "Paused"); _; } uses require with negation and includes '_;' to run the function. modifier whenNotPaused() { if(paused) { revert("Paused"); } } lacks '_;', so function never runs. modifier whenNotPaused() { require(paused, "Paused"); _; } requires paused to be true, which is opposite. modifier whenNotPaused() { if(!paused) { _; } } runs function only if not paused but lacks else handling and is less clear.
  3. Final Answer:

    modifier whenNotPaused() { require(!paused, "Paused"); _; } -> Option A
  4. Quick Check:

    Require !paused with '_;' runs function [OK]
Hint: Use require(!paused) and include '_;' to run function [OK]
Common Mistakes:
  • Reversing paused condition in require
  • Omitting '_;' causing function not to run
  • Using if without else or revert
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard