0
0
Blockchain / Solidityprogramming~20 mins

msg.value and msg.sender in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of msg.value and msg.sender
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 event log?

Consider the following Solidity contract snippet. A user calls sendEther with 1 ether. What will be the values emitted in the event?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    event Log(address sender, uint amount);

    function sendEther() public payable {
        emit Log(msg.sender, msg.value);
    }
}
AThe event logs the contract's address and zero ether.
BThe event logs the caller's address and zero ether.
CThe event logs the caller's address and the amount of ether sent (1 ether).
DThe event logs the zero address and the amount of ether sent.
Attempts:
2 left
💡 Hint

Remember, msg.sender is the caller's address and msg.value is the amount of ether sent with the call.

🧠 Conceptual
intermediate
1:30remaining
What does msg.value represent in Solidity?

Choose the best description of msg.value in a Solidity function call.

A<code>msg.value</code> is the amount of ether (in wei) sent with the transaction.
B<code>msg.value</code> is the block timestamp when the transaction was mined.
C<code>msg.value</code> is the address of the sender.
D<code>msg.value</code> is the amount of gas used by the transaction.
Attempts:
2 left
💡 Hint

Think about what value is transferred along with a payable function call.

Predict Output
advanced
2:00remaining
What happens if a non-payable function tries to access msg.value with ether sent?

Given this contract, what will happen if nonPayableFunction is called with 1 ether?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    function nonPayableFunction() public {
        uint amount = msg.value;
    }
}
AThe transaction will revert because the function is not payable but ether was sent.
BThe function will run and <code>amount</code> will be 1 ether.
CThe function will run and <code>amount</code> will be zero.
DThe function will ignore <code>msg.value</code> and continue without error.
Attempts:
2 left
💡 Hint

Consider the payable modifier and what happens when ether is sent to a non-payable function.

🧠 Conceptual
advanced
1:30remaining
Which statement about msg.sender is true?

Choose the correct statement about msg.sender in Solidity.

A<code>msg.sender</code> is the address of the contract currently executing the code.
B<code>msg.sender</code> can be a contract address if the call was made by another contract.
C<code>msg.sender</code> is always the original external account that started the transaction.
D<code>msg.sender</code> is always the zero address when called internally.
Attempts:
2 left
💡 Hint

Think about calls between contracts and external accounts.

🔧 Debug
expert
2:30remaining
Why does this contract fail to receive ether?

Examine the contract below. Why does sending ether to receiveEther fail?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    function receiveEther() public {
        uint received = msg.value;
    }
}
AThe function does not emit an event to confirm ether reception.
BThe function does not have a fallback function to accept ether.
CThe contract does not have a constructor to initialize ether reception.
DThe function is not marked payable, so it cannot receive ether.
Attempts:
2 left
💡 Hint

Check the function modifiers required to accept ether.