0
0
Blockchain / Solidityprogramming~20 mins

Receive and fallback functions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Receive and Fallback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
    }
}
ATransaction reverts with an error.
BThe event 'fallback called' is emitted.
CThe event 'receive called' is emitted.
DNo event is emitted, but Ether is accepted.
Attempts:
2 left
💡 Hint
Sending Ether without data triggers the receive function if it exists.
Predict Output
intermediate
2: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");
    }
}
AThe event 'fallback called' is emitted.
BTransaction reverts with an error.
CNo event is emitted, Ether is refunded.
DThe event 'receive called' is emitted.
Attempts:
2 left
💡 Hint
Fallback is called when calldata is not empty and no matching function exists.
🔧 Debug
advanced
2: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
    }
}
ABecause fallback() is missing the receive() function, it reverts.
BBecause fallback() is not payable, it rejects Ether transfers.
CBecause fallback() has no code, it reverts automatically.
DBecause the contract has no constructor, it rejects Ether.
Attempts:
2 left
💡 Hint
Only payable functions can receive Ether.
📝 Syntax
advanced
2: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
    }
}
AAdd empty braces: fallback() external payable {}
BAdd a semicolon after fallback() external payable;
CRemove payable modifier: fallback() external {}
DAdd returns keyword: fallback() external payable returns (bool) {}
Attempts:
2 left
💡 Hint
Functions must have a body or be declared abstract.
🚀 Application
expert
2: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;
    }
}
AFallback is called twice, count becomes 2.
BFallback is called once, but count remains 0 due to no Ether.
CFallback is not called, count remains 0.
DFallback is called once, count becomes 1.
Attempts:
2 left
💡 Hint
Fallback is called once per transaction if calldata does not match any function.