Challenge - 5 Problems
Function Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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!"; } }
Attempts:
2 left
💡 Hint
Think about what the modifier checks before allowing the function to run.
✗ Incorrect
The modifier onlyOnce allows the function to run only if message is empty. On first call, message is empty, so test() sets it to "Hello, blockchain!".
❓ Predict Output
intermediate2: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")); } }
Attempts:
2 left
💡 Hint
Modifiers run in the order they are listed before the function body.
✗ Incorrect
Modifiers run in order: first adds "1", then second adds "2", then function adds "E". So log becomes "12E".
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Check if the contract has Ether to send before calling transfer.
✗ Incorrect
The contract has a balance variable but no actual Ether. Calling transfer fails because contract's Ether balance is zero.
📝 Syntax
advanced2: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);
_;
}Attempts:
2 left
💡 Hint
Statements in Solidity must end with a semicolon.
✗ Incorrect
The require statement is missing a semicolon at the end, causing a syntax error.
🚀 Application
expert3: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 } }
Attempts:
2 left
💡 Hint
Consider the order modifiers run and how counter is updated.
✗ Incorrect
Modifiers run in order: onlyOwner checks sender, then incrementCounter runs function body (empty), then increments counter. Each call increments counter by 1, so after two calls counter is 2.