What is the purpose of the receive function in a smart contract?
The receive function is a special function in Solidity that is executed when the contract receives plain Ether without any data. It allows the contract to accept Ether transfers.
Click to reveal answer
beginner
When is the fallback function triggered in a smart contract?
The fallback function is called when a contract receives a call with data that does not match any function signature or when Ether is sent but no receive function is defined or if the receive function is not payable.
Click to reveal answer
intermediate
Can a contract have both receive and fallback functions? What happens if both exist?
Yes, a contract can have both. The receive function handles plain Ether transfers with empty calldata, while the fallback function handles calls with data or Ether transfers when receive is not present or not payable.
Click to reveal answer
beginner
What visibility and modifiers must the receive function have?
The receive function must be declared as external and payable to accept Ether transfers.
Click to reveal answer
beginner
What happens if a contract does not have a receive or fallback function and it receives Ether?
The transaction will fail and revert because the contract cannot accept Ether without a receive or fallback function marked payable.
Click to reveal answer
Which function is called when a contract receives Ether with empty calldata?
Aconstructor()
Bfallback()
Cpayable()
Dreceive()
✗ Incorrect
The receive() function is triggered when Ether is sent with empty calldata.
What visibility must the fallback function have?
Aexternal
Binternal
Cprivate
Dpublic
✗ Incorrect
The fallback function must be declared external.
If a contract has both receive() and fallback(), which handles calls with data?
Areceive()
Bfallback()
CBoth equally
DNeither
✗ Incorrect
fallback() handles calls with data or unknown function signatures.
What happens if a contract without receive() or fallback() receives Ether?
AEther is stored in fallback
BEther is accepted silently
CTransaction reverts
DContract self-destructs
✗ Incorrect
Without receive() or payable fallback(), Ether transfers fail and revert.
Which modifier must be used on receive() to accept Ether?
Apayable
Bview
Cpure
Dvirtual
✗ Incorrect
The receive() function must be payable to accept Ether.
Explain the difference between the receive() and fallback() functions in Solidity.
Think about when each function is triggered based on the data sent.
You got /4 concepts.
Describe what happens when a contract without receive() or fallback() functions receives Ether.
Consider the contract's ability to accept Ether without special functions.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of the receive() function in a Solidity contract?
easy
A. To reject all incoming Ether transfers
B. To handle calls to unknown functions with data
C. To execute code when a contract is created
D. To handle plain Ether transfers without any data
Solution
Step 1: Understand the role of receive()
The receive() function is designed to accept Ether sent to the contract without any accompanying data.
Step 2: Differentiate from fallback function
The fallback function handles calls with data or unknown function calls, while receive() is only for plain Ether transfers.
Final Answer:
To handle plain Ether transfers without any data -> Option D
Quick Check:
receive() handles plain Ether = C [OK]
Hint: receive() handles Ether with no data, fallback handles calls with data [OK]
Common Mistakes:
Confusing receive() with fallback()
Thinking receive() handles calls with data
Assuming receive() rejects Ether
2.
Which of the following is the correct syntax for a receive() function in Solidity?
?
easy
A. receive() external payable {}
B. function receive() public {}
C. fallback() external payable {}
D. function receive() external {}
Solution
Step 1: Recall receive() function syntax
The receive() function must be declared without the function keyword, must be external and payable.
Step 2: Check each option
receive() external payable {} matches the correct syntax. Options using the function keyword are invalid, as are those lacking payable or using fallback instead of receive.
Final Answer:
receive() external payable {} -> Option A
Quick Check:
receive() syntax requires external payable = B [OK]
Hint: receive() has no function keyword, must be external payable [OK]