Challenge - 5 Problems
Receive and Fallback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What happens when Ether is sent to this contract?
Consider the following Solidity contract. What will be the output when 1 Ether is sent to the contract address without calling any function explicitly?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Test { event Log(string func); receive() external payable { emit Log("receive called"); } fallback() external payable { emit Log("fallback called"); } }
Attempts:
2 left
💡 Hint
Sending Ether without data triggers the receive function if it exists.
✗ Incorrect
When Ether is sent with empty calldata, the receive() function is called if it is defined and payable. Otherwise, fallback() is called if payable. Here, receive() exists and is payable, so it emits 'receive called'.
❓ Predict Output
intermediate2:00remaining
Which function is called when sending data to this contract?
Given the contract below, what event is emitted when a transaction with non-empty calldata but no matching function signature is sent?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Test { event Log(string func); receive() external payable { emit Log("receive called"); } fallback() external payable { emit Log("fallback called"); } }
Attempts:
2 left
💡 Hint
Fallback is called when calldata is not empty and no matching function exists.
✗ Incorrect
When calldata is non-empty and no matching function exists, the fallback() function is called if payable. Here, fallback() emits 'fallback called'.
🔧 Debug
advanced2:00remaining
Why does this contract reject Ether transfers?
Examine the contract below. Why does sending Ether to this contract cause the transaction to revert?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
fallback() external {
// no payable modifier
}
}Attempts:
2 left
💡 Hint
Only payable functions can receive Ether.
✗ Incorrect
The fallback() function is not marked payable, so any Ether sent to the contract triggers a revert because no payable receive() or fallback() exists.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this fallback function
Which option correctly fixes the syntax error in the fallback function below?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
fallback() external payable {
// missing function body
}
}Attempts:
2 left
💡 Hint
Functions must have a body or be declared abstract.
✗ Incorrect
The fallback function must have a function body enclosed in braces. Adding empty braces fixes the syntax error.
🚀 Application
expert2:00remaining
How many times is fallback called in this scenario?
Given the contract below, if a user sends 0 Ether with non-empty calldata that does not match any function, how many times will the fallback function be called during the transaction?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Test { uint public count = 0; fallback() external payable { count++; } function getCount() public view returns (uint) { return count; } }
Attempts:
2 left
💡 Hint
Fallback is called once per transaction if calldata does not match any function.
✗ Incorrect
The fallback function is called once per transaction when calldata does not match any function. The count increments by 1 regardless of Ether sent.