0
0
Blockchain / Solidityprogramming~5 mins

Require, assert, and revert in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the require statement do in a smart contract?

require checks a condition and stops execution if the condition is false. It refunds remaining gas and returns an error message.

Click to reveal answer
intermediate
How is assert different from require?

assert is used to check for conditions that should never be false. If it fails, it consumes all remaining gas and indicates a serious error.

Click to reveal answer
beginner
What happens when revert is called in a smart contract?

revert immediately stops execution, undoes all changes, and refunds remaining gas. It can include a custom error message.

Click to reveal answer
intermediate
When should you use require versus assert?

Use require to check user inputs or external conditions. Use assert to check for internal errors or invariants that should never fail.

Click to reveal answer
advanced
Why is gas refunded when require or revert fails but not when assert fails?

require and revert indicate expected errors and refund gas to save users money. assert signals a critical bug, so it consumes all gas to highlight the problem.

Click to reveal answer
Which statement immediately stops execution and refunds remaining gas if a condition is false?
Acontinue
Bassert
Crevert
Drequire
Which statement is used to check for conditions that should never happen and consumes all gas if it fails?
Aassert
Brequire
Crevert
Dthrow
What does revert do in a smart contract?
AContinues execution ignoring errors
BStops execution and refunds gas with an optional error message
CConsumes all gas and stops execution
DLogs a warning but continues
When should you use require in your smart contract?
ATo check user inputs and external conditions
BTo check internal invariants
CTo consume all gas on error
DTo log events
Why does assert consume all remaining gas on failure?
ABecause it is used for user input validation
BBecause it refunds gas to the user
CBecause it signals a critical bug that should never happen
DBecause it logs an error
Explain the differences between require, assert, and revert in smart contracts.
Think about when each is used and how gas is handled.
You got /3 concepts.
    When should you use require instead of assert in your code?
    Consider the source of the error.
    You got /2 concepts.