0
0
Blockchain / Solidityprogramming~20 mins

Require, assert, and revert in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Require, Assert, and Revert
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 Solidity function call?

Consider the following Solidity function:

function testRequire(uint x) public pure returns (string memory) {
    require(x > 10, "x must be greater than 10");
    return "Passed require";
}

What happens when testRequire(5) is called?

Blockchain / Solidity
function testRequire(uint x) public pure returns (string memory) {
    require(x > 10, "x must be greater than 10");
    return "Passed require";
}
AThe function returns 'Passed require'.
BThe transaction reverts with error message 'x must be greater than 10'.
CThe function returns an empty string.
DThe transaction succeeds but emits a warning.
Attempts:
2 left
💡 Hint

Remember, require checks a condition and reverts if false.

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

Analyze this Solidity function:

function testAssert(uint x) public pure returns (string memory) {
    assert(x != 0);
    return "Passed assert";
}

What happens when testAssert(0) is called?

Blockchain / Solidity
function testAssert(uint x) public pure returns (string memory) {
    assert(x != 0);
    return "Passed assert";
}
AThe transaction reverts with an 'assertion failed' error.
BThe function returns 'Passed assert'.
CThe function returns an empty string.
DThe transaction reverts with a custom error message.
Attempts:
2 left
💡 Hint

assert is used to check for conditions that should never be false.

Predict Output
advanced
2:00remaining
What is the output of this Solidity function using revert?

Consider this Solidity function:

function testRevert(uint x) public pure returns (string memory) {
    if (x < 5) {
        revert("x must be at least 5");
    }
    return "Passed revert";
}

What happens when testRevert(3) is called?

Blockchain / Solidity
function testRevert(uint x) public pure returns (string memory) {
    if (x < 5) {
        revert("x must be at least 5");
    }
    return "Passed revert";
}
AThe transaction reverts with error message 'x must be at least 5'.
BThe function returns 'Passed revert'.
CThe function returns an empty string.
DThe transaction succeeds but emits a warning.
Attempts:
2 left
💡 Hint

revert immediately stops execution and reverts the transaction with a message.

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

Look at this Solidity function:

function checkValue(uint x) public pure returns (string memory) {
    require(x > 0, "x must be positive");
    assert(x < 10);
    if (x >= 10) {
        revert("x is too large");
    }
    return "Value is valid";
}

Why does calling checkValue(10) always revert?

Blockchain / Solidity
function checkValue(uint x) public pure returns (string memory) {
    require(x > 0, "x must be positive");
    assert(x < 10);
    if (x >= 10) {
        revert("x is too large");
    }
    return "Value is valid";
}
ABecause the require fails when x is 10.
BBecause the revert statement is always executed regardless of x.
CBecause the assert fails when x is 10, causing a revert before the revert statement.
DBecause the function returns an invalid string causing revert.
Attempts:
2 left
💡 Hint

Check the order of assert and revert statements.

🧠 Conceptual
expert
2:00remaining
Which statement about require, assert, and revert is TRUE?

Choose the correct statement about require, assert, and revert in Solidity:

A<code>assert</code> should be used to check for conditions that should never be false and consumes all remaining gas on failure.
B<code>require</code> is used only for internal errors, while <code>assert</code> is for user input validation.
C<code>revert</code> cannot include an error message, unlike <code>require</code> and <code>assert</code>.
D<code>require</code> and <code>revert</code> both refund remaining gas when they fail, but <code>assert</code> does not.
Attempts:
2 left
💡 Hint

Think about gas consumption and error messages for each statement.