0
0
Blockchain / Solidityprogramming~20 mins

Payable functions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Payable Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity payable function call?
Consider the following Solidity contract snippet. What will be the value of contractBalance after calling deposit with 1 ether?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Wallet {
    uint public contractBalance;

    function deposit() public payable {
        contractBalance += msg.value;
    }
}
AcontractBalance will be 0 because msg.value is not added
BcontractBalance will be 1000000000000000000 (1 ether in wei)
CcontractBalance will be 1 because ether is counted as 1
DThe code will not compile due to missing payable keyword
Attempts:
2 left
💡 Hint
Remember that msg.value holds the amount of wei sent with the transaction.
Predict Output
intermediate
2:00remaining
What error occurs when calling a non-payable function with ether?
Given this Solidity contract, what happens if you call withdraw and send 1 ether along with the call?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Bank {
    function withdraw() public {
        // withdraw logic
    }
}
ATransaction reverts with error: 'non-payable function cannot accept ether'
BTransaction succeeds and ether is accepted
CTransaction reverts with 'out of gas' error
DTransaction succeeds but ether is ignored
Attempts:
2 left
💡 Hint
Check if the function is marked payable or not.
🔧 Debug
advanced
2:00remaining
Why does this payable fallback function not receive ether?
Examine the contract below. Why does sending ether to this contract fail to increase its balance?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    fallback() external {
        // fallback without payable
    }
}
AFallback function is not marked payable, so it rejects ether
BFallback function must be named receive to accept ether
CFallback functions cannot be external
DContract balance cannot increase without a deposit function
Attempts:
2 left
💡 Hint
Check the fallback function signature and payable keyword.
🧠 Conceptual
advanced
1:30remaining
Which statement about payable functions is true?
Select the correct statement about payable functions in Solidity.
AAll functions can receive ether regardless of payable keyword
BPayable keyword is optional if the contract has a receive function
CPayable functions automatically reject ether sent
DOnly functions marked payable can receive ether during a call
Attempts:
2 left
💡 Hint
Think about how Solidity controls ether transfers to functions.
Predict Output
expert
2:30remaining
What is the contract balance after these calls?
Given the contract below, what is the value of address(this).balance after these calls? 1. Call deposit with 2 ether 2. Call withdraw with 1 ether Assume withdraw sends ether back to the caller.
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Vault {
    function deposit() public payable {
        // Accept ether
    }

    function withdraw(uint amount) public {
        payable(msg.sender).transfer(amount);
    }
}
A0 ether
B2 ether (2000000000000000000 wei)
C1 ether (1000000000000000000 wei)
D3 ether
Attempts:
2 left
💡 Hint
Consider how much ether is sent and withdrawn from the contract balance.