0
0
Blockchain / Solidityprogramming~5 mins

Virtual and override keywords in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the virtual keyword do in a blockchain smart contract?
It marks a function that can be changed (overridden) by a contract that inherits from this one. Think of it like a placeholder that child contracts can customize.
Click to reveal answer
beginner
What is the purpose of the override keyword in a derived contract?
It tells the blockchain compiler that this function replaces a virtual function from a parent contract. It’s like saying, "I’m changing how this works here."
Click to reveal answer
intermediate
Can a function be overridden if it is not marked as virtual in the parent contract?
No. Only functions marked as virtual can be overridden. This protects important functions from accidental changes.
Click to reveal answer
intermediate
Why is it important to use override explicitly when changing a function?
It helps avoid mistakes by making sure you really mean to replace a parent function. The compiler checks this and gives an error if you forget it.
Click to reveal answer
beginner
Example: What happens if a child contract overrides a virtual function but forgets to use override keyword?
The compiler will give an error and won’t let the contract compile. This prevents accidental mistakes in your smart contract code.
Click to reveal answer
What keyword must a parent contract use to allow a function to be changed by a child contract?
Avirtual
Boverride
Cfinal
Dstatic
Which keyword must a child contract use when it changes a parent's virtual function?
Aoverride
Bvirtual
Cabstract
Dstatic
What happens if you override a function but forget to use override keyword?
AThe contract compiles fine
BThe compiler gives an error
CThe function runs twice
DThe function is ignored
Can a function without virtual be overridden?
AYes, always
BOnly if it is private
COnly if marked <code>override</code>
DNo, it cannot be overridden
Why do blockchain languages require explicit virtual and override keywords?
ATo make code run faster
BTo allow private functions
CTo prevent accidental function changes
DTo save storage space
Explain in your own words how the virtual and override keywords work together in blockchain smart contracts.
Think about how a parent contract allows children to customize behavior safely.
You got /4 concepts.
    Why is it important to mark functions as virtual before allowing them to be overridden?
    Consider what could happen if any function could be changed without warning.
    You got /4 concepts.