Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Receiving Ether in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ether Receiver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when sending Ether to this contract?

Consider the following Solidity contract that can receive Ether. What will be the value of received after sending 1 Ether?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Receiver {
    uint public received = 0;

    receive() external payable {
        received = msg.value;
    }
}
Areceived will be 1 ether (1000000000000000000 wei)
Breceived will be 0
Creceived will be the sender's address
DThe contract will reject the Ether and revert
Attempts:
2 left
💡 Hint

The receive() function is called when Ether is sent without data. msg.value holds the amount of Ether sent in wei.

❓ Predict Output
intermediate
2:00remaining
What happens if a contract has no receive or fallback function and receives Ether?

Given this contract, what happens when it receives Ether?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract NoReceive {
    uint public balance = 0;
}
AThe contract accepts Ether but balance stays zero
BThe contract accepts Ether and balance updates automatically
CThe contract rejects the Ether and the transaction reverts
DThe contract throws a warning but accepts Ether
Attempts:
2 left
💡 Hint

Contracts without receive() or payable fallback() cannot accept plain Ether transfers.

🔧 Debug
advanced
2:00remaining
Why does this contract fail to receive Ether?

Examine the contract below. It is supposed to receive Ether and update receivedAmount. Why does sending Ether fail?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract ReceiverFail {
    uint public receivedAmount;

    fallback() external {
        receivedAmount = msg.value;
    }
}
AThe contract does not have a constructor to initialize receivedAmount
BThe fallback function is not marked payable, so it rejects Ether
Cmsg.value is always zero in fallback functions
DThe fallback function should be named receive to accept Ether
Attempts:
2 left
💡 Hint

To accept Ether, fallback or receive functions must be marked payable.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this receive function

What is wrong with this receive function code?

Blockchain / Solidity
receive() external {
    // receive Ether
}
AMissing the payable keyword in the receive function declaration
BThe receive function cannot be external
CThe receive function must have a return type
DThe receive function must have a function name
Attempts:
2 left
💡 Hint

Receive functions must be able to accept Ether.

🚀 Application
expert
2:00remaining
How many times can a contract receive Ether in this scenario?

Consider this contract deployed on Ethereum. A user sends 0.5 Ether twice in separate transactions. How many times is the receive() function called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract MultiReceive {
    uint public count = 0;

    receive() external payable {
        count++;
    }
}
AIt depends on the gas limit of the transactions
B1 time, because the contract only accepts Ether once
C0 times, receive is only called on contract creation
D2 times, once for each Ether transfer
Attempts:
2 left
💡 Hint

The receive() function is triggered every time Ether is sent without data.

Practice

(1/5)
1.

What is the main purpose of the receive function in a Solidity contract?

easy
A. To reject incoming Ether transfers
B. To send Ether from the contract to an external address
C. To initialize contract state variables
D. To accept Ether sent to the contract without any data

Solution

  1. Step 1: Understand the role of the receive function

    The receive function is a special function in Solidity that is triggered when the contract receives Ether without any calldata.
  2. Step 2: Identify its purpose

    Its purpose is to accept Ether sent directly to the contract without any function call data.
  3. Final Answer:

    To accept Ether sent to the contract without any data -> Option D
  4. Quick Check:

    receive function = accept Ether without data [OK]
Hint: Remember: receive handles plain Ether transfers only [OK]
Common Mistakes:
  • Confusing receive with fallback function
  • Thinking receive sends Ether out
  • Assuming receive initializes variables
2.

Which of the following is the correct syntax for a receive function in Solidity?

?
easy
A. receive() internal payable {}
B. receive() external payable {}
C. receive() external {}
D. receive() public payable {}

Solution

  1. Step 1: Recall receive function requirements

    The receive function must be declared as external and payable to accept Ether without data.
  2. Step 2: Match syntax

    receive() external payable {} matches the required syntax: receive() external payable {}.
  3. Final Answer:

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

    receive must be external and payable [OK]
Hint: receive must be external and payable, no public or internal [OK]
Common Mistakes:
  • Using public instead of external
  • Omitting payable keyword
  • Making receive internal or private
3.

Consider this Solidity contract snippet:

contract Test {
    event Received(address sender, uint amount);
    receive() external payable {
        emit Received(msg.sender, msg.value);
    }
}

What happens when someone sends 1 Ether to this contract without calling any function?

medium
A. The contract rejects the Ether and transaction fails
B. The contract accepts Ether but does not emit any event
C. The contract accepts the Ether and emits the Received event
D. The contract calls fallback function instead

Solution

  1. Step 1: Analyze the receive function

    The receive function is payable and emits an event with sender and amount when Ether is received without data.
  2. Step 2: Understand the behavior on Ether transfer

    Sending 1 Ether without data triggers receive, so Ether is accepted and event emitted.
  3. Final Answer:

    The contract accepts the Ether and emits the Received event -> Option C
  4. Quick Check:

    receive payable + emit event = accept and notify [OK]
Hint: Sending Ether without data triggers receive, not fallback [OK]
Common Mistakes:
  • Thinking fallback is called instead
  • Assuming no event is emitted
  • Believing Ether is rejected
4.

Identify the error in this Solidity contract snippet that prevents it from receiving Ether:

contract Example {
    receive() public payable {
        // code
    }
}
medium
A. receive function must be external, not public
B. receive function must not be payable
C. receive function must have a return type
D. receive function must be internal

Solution

  1. Step 1: Check receive function visibility

    The receive function must be declared as external, not public.
  2. Step 2: Confirm payable requirement

    It is correctly marked payable, so no error there.
  3. Final Answer:

    receive function must be external, not public -> Option A
  4. Quick Check:

    receive must be external, not public [OK]
Hint: receive must be external, not public or internal [OK]
Common Mistakes:
  • Using public instead of external
  • Removing payable keyword
  • Adding return type to receive
5.

You want to create a contract that tracks the total Ether received and rejects any Ether sent with data. Which combination of functions should you implement?

hard
A. Implement receive() external payable to accept Ether and fallback() external to revert on data
B. Implement only fallback() external payable to accept all Ether
C. Implement receive() external without payable and fallback() external payable
D. Implement receive() external payable and fallback() external payable both accepting Ether

Solution

  1. Step 1: Accept Ether without data using receive

    The receive function accepts Ether sent without data and can update total received.
  2. Step 2: Reject Ether sent with data using fallback

    The fallback function should revert to reject any Ether sent with data.
  3. Final Answer:

    Implement receive() external payable to accept Ether and fallback() external to revert on data -> Option A
  4. Quick Check:

    receive accepts plain Ether, fallback rejects data [OK]
Hint: Use receive for plain Ether, fallback to reject data [OK]
Common Mistakes:
  • Making fallback payable and accepting Ether with data
  • Omitting fallback to reject data
  • Not marking receive payable
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