0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 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.