Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

Receive and fallback functions in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
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()
What visibility must the fallback function have?
Aexternal
Binternal
Cprivate
Dpublic
If a contract has both receive() and fallback(), which handles calls with data?
Areceive()
Bfallback()
CBoth equally
DNeither
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
Which modifier must be used on receive() to accept Ether?
Apayable
Bview
Cpure
Dvirtual
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

      1. Step 1: Understand the role of receive()

        The receive() function is designed to accept Ether sent to the contract without any accompanying data.
      2. 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.
      3. Final Answer:

        To handle plain Ether transfers without any data -> Option D
      4. 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

      1. Step 1: Recall receive() function syntax

        The receive() function must be declared without the function keyword, must be external and payable.
      2. 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.
      3. Final Answer:

        receive() external payable {} -> Option A
      4. Quick Check:

        receive() syntax requires external payable = B [OK]
      Hint: receive() has no function keyword, must be external payable [OK]
      Common Mistakes:
      • Adding 'function' keyword to receive()
      • Omitting 'payable' modifier
      • Confusing fallback() with receive()
      3.

      Consider this Solidity contract snippet:

      contract Test {
          event Log(string func);
      
          receive() external payable {
              emit Log("receive");
          }
      
          fallback() external payable {
              emit Log("fallback");
          }
      }

      What event is emitted when someone sends 1 Ether with empty data?

      medium
      A. "receive"
      B. "fallback"
      C. No event emitted
      D. Compilation error

      Solution

      1. Step 1: Identify the call type

        Sending Ether with empty data triggers the receive() function if it exists.
      2. Step 2: Check which event is emitted

        The receive() function emits the event with "receive" string.
      3. Final Answer:

        "receive" -> Option A
      4. Quick Check:

        Empty data + Ether triggers receive() = "receive" [OK]
      Hint: Empty data + Ether calls receive(), else fallback [OK]
      Common Mistakes:
      • Assuming fallback() triggers on empty data
      • Thinking no event is emitted
      • Confusing receive() and fallback() roles
      4.

      Given this contract snippet, what is the error?

      contract Example {
          fallback() external {
              // code
          }
      
          receive() external {
              // code
          }
      }
      medium
      A. receive() cannot be declared after fallback()
      B. Both fallback and receive must be payable to accept Ether
      C. fallback() must have the function keyword
      D. No error, code is correct

      Solution

      1. Step 1: Check payable modifiers

        To accept Ether, both receive() and fallback() must be marked payable.
      2. Step 2: Analyze given code

        Both functions lack payable, so contract cannot receive Ether via these functions.
      3. Final Answer:

        Both fallback and receive must be payable to accept Ether -> Option B
      4. Quick Check:

        Missing payable means no Ether acceptance = A [OK]
      Hint: Missing payable means no Ether accepted by receive or fallback [OK]
      Common Mistakes:
      • Thinking order of functions matters
      • Assuming fallback needs 'function' keyword
      • Ignoring payable requirement
      5.

      You want your contract to accept Ether sent with or without data, and log which function was called. Which is the best way to implement this?

      ?
      hard
      A. Do not implement receive or fallback; use a regular payable function instead
      B. Implement only receive() external payable function and ignore fallback
      C. Implement both receive() external payable and fallback() external payable functions, each emitting a distinct event
      D. Implement only fallback() external payable function to handle all cases

      Solution

      1. Step 1: Understand Ether transfer scenarios

        Ether can be sent with empty data (calls receive) or with data (calls fallback).
      2. Step 2: Implement both functions to handle all cases

        Implementing both receive() and fallback() as external payable lets the contract accept Ether in both cases and emit distinct events.
      3. Final Answer:

        Implement both receive() external payable and fallback() external payable functions, each emitting a distinct event -> Option C
      4. Quick Check:

        Both functions needed for all Ether types = D [OK]
      Hint: Use both receive() and fallback() payable to cover all Ether sends [OK]
      Common Mistakes:
      • Implementing only one function and missing some Ether transfers
      • Ignoring fallback for calls with data
      • Using regular payable function instead of receive/fallback
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard