Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Payable functions in Blockchain / Solidity - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the function accept Ether payments.

Blockchain / Solidity
function deposit() public [1] {
    // Function body
}
Drag options to blanks, or click blank then click option'
Apure
Bexternal
Cview
Dpayable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' which do not allow Ether transfers.
Forgetting to add 'payable' causes the function to reject Ether.
2fill in blank
medium

Complete the code to access the amount of Ether sent to the function.

Blockchain / Solidity
function deposit() public payable {
    uint amount = [1];
}
Drag options to blanks, or click blank then click option'
Amsg.value
Bmsg.sender
Caddress(this).balance
Dtx.gasprice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'msg.sender' which is the sender's address, not the amount.
Using 'address(this).balance' which is the contract's total balance, not the sent amount.
3fill in blank
hard

Fix the error in the function declaration to allow receiving Ether.

Blockchain / Solidity
function receiveFunds() public [1] {
    // Accept Ether
}
Drag options to blanks, or click blank then click option'
Apure
Bview
Cpayable
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' which prevent Ether transfers.
Omitting 'payable' causes the function to reject Ether.
4fill in blank
hard

Fill both blanks to create a payable fallback function that accepts Ether.

Blockchain / Solidity
fallback() [1] [2] {
    // fallback logic
}
Drag options to blanks, or click blank then click option'
Apayable
Bview
Cpure
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Marking fallback as 'view' or 'pure' which is invalid.
Omitting 'payable' causes fallback to reject Ether.
5fill in blank
hard

Fill all three blanks to create a payable receive function that accepts Ether and updates balance.

Blockchain / Solidity
mapping(address => uint) public balances;

receive() external [1] {
    balances[msg.sender] [2]= [3];
}
Drag options to blanks, or click blank then click option'
Apayable
B+
Cmsg.value
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'payable' on the receive function.
Using '=' instead of '+=' which overwrites balance.
Using 'msg.sender' instead of 'msg.value' for amount.

Practice

(1/5)
1. What is the main purpose of a payable function in a blockchain smart contract?
easy
A. To allow the function to receive cryptocurrency along with the call
B. To make the function run faster
C. To restrict the function to only the contract owner
D. To prevent the function from modifying state variables

Solution

  1. Step 1: Understand the role of payable

    The payable keyword marks a function that can accept cryptocurrency (like Ether) when called.
  2. Step 2: Compare other options

    Options A, C, and D describe unrelated behaviors. Only To allow the function to receive cryptocurrency along with the call correctly describes the purpose of payable.
  3. Final Answer:

    To allow the function to receive cryptocurrency along with the call -> Option A
  4. Quick Check:

    Payable = Receive money [OK]
Hint: Payable means function can accept money sent [OK]
Common Mistakes:
  • Thinking payable makes function faster
  • Confusing payable with access control
  • Believing payable prevents state changes
2. Which of the following is the correct syntax to declare a payable function in Solidity?
easy
A. payable function deposit() public {}
B. function deposit() payable public {}
C. function payable deposit() public {}
D. function deposit() public payable {}

Solution

  1. Step 1: Recall Solidity function declaration order

    In Solidity, the visibility modifier (e.g., public) comes before payable keyword.
  2. Step 2: Check each option

    function deposit() public payable {} uses correct order: function deposit() public payable {}. Options A, B, and D have incorrect keyword order or syntax.
  3. Final Answer:

    function deposit() public payable {} -> Option D
  4. Quick Check:

    Correct order: visibility then payable [OK]
Hint: Visibility modifier before payable keyword [OK]
Common Mistakes:
  • Placing payable before function keyword
  • Switching order of public and payable
  • Using payable as a function name
3. What will be the output of the following Solidity function when called with 2 Ether?
contract Test {
    uint public balance;
    function deposit() public payable {
        balance = msg.value;
    }
}
medium
A. balance will be 2000000000000000000 (2 Ether in wei)
B. balance will be 2
C. Compilation error
D. balance will be 0

Solution

  1. Step 1: Understand msg.value in Solidity

    msg.value holds the amount of wei (smallest Ether unit) sent with the transaction. 2 Ether equals 2 * 10^18 wei = 2000000000000000000 wei.
  2. Step 2: Analyze the assignment to balance

    The function sets balance = msg.value, so balance stores the wei amount sent, which is 2000000000000000000.
  3. Final Answer:

    balance will be 2000000000000000000 (2 Ether in wei) -> Option A
  4. Quick Check:

    msg.value stores wei amount sent [OK]
Hint: msg.value is in wei, not Ether [OK]
Common Mistakes:
  • Assuming msg.value is in Ether
  • Expecting balance to be zero
  • Thinking function won't compile
4. Identify the error in this Solidity function that is intended to receive Ether:
function receiveFunds() public {
    require(msg.value > 0, "Send some Ether");
}
medium
A. require condition is incorrect
B. Function must be marked payable to receive Ether
C. Function visibility should be private
D. msg.value cannot be used inside functions

Solution

  1. Step 1: Check function declaration for payable

    To receive Ether, a function must be declared with the payable keyword. This function lacks it.
  2. Step 2: Validate other options

    The require condition is valid, visibility can be public, and msg.value is accessible in payable functions.
  3. Final Answer:

    Function must be marked payable to receive Ether -> Option B
  4. Quick Check:

    Missing payable causes Ether rejection [OK]
Hint: Add payable to accept Ether in function [OK]
Common Mistakes:
  • Forgetting payable keyword
  • Misusing require condition
  • Changing visibility unnecessarily
5. You want to create a smart contract function that accepts Ether and updates the sender's balance only if at least 1 Ether is sent. Which of the following implementations correctly achieves this?
hard
A. function deposit() payable public { if (msg.value > 1) { balances[msg.sender] = msg.value; } }
B. function deposit() public { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; }
C. function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; }
D. function deposit() public payable { if (msg.value >= 1) { balances[msg.sender] += msg.value; } }

Solution

  1. Step 1: Check for payable keyword and require condition

    function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } correctly marks the function as payable and uses require(msg.value >= 1 ether) to enforce minimum Ether sent.
  2. Step 2: Validate balance update and syntax

    function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } updates balances[msg.sender] by adding msg.value. Other options either miss payable, use wrong comparison, or incorrect balance update.
  3. Final Answer:

    function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } -> Option C
  4. Quick Check:

    Payable + require with 1 ether check [OK]
Hint: Use require with 1 ether and payable function [OK]
Common Mistakes:
  • Omitting payable keyword
  • Using incorrect Ether comparison
  • Not updating balance correctly
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