Solidity Basic Solidity (Web Dev) 2 — Questions and Answers
Question 1: Which keyword is used to declare a function that cannot modify contract state in Solidity?
- pure
- view (Correct answer)
- constant
- readonly
Correct answer: view
The 'view' keyword declares a function that reads but does not modify contract state.
Question 2: What does the 'payable' modifier on a function allow in Solidity?
- The function can call other contracts
- The function can receive Ether (Correct answer)
- The function can modify storage
- The function can be called externally only
Correct answer: The function can receive Ether
The 'payable' modifier allows a function to receive Ether sent along with the call.
Question 3: Which of the following is the correct way to declare a mapping from address to uint in Solidity?
- mapping(uint => address) balances;
- map<address, uint> balances;
- mapping(address => uint) balances; (Correct answer)
- dict(address: uint) balances;
Correct answer: mapping(address => uint) balances;
Solidity mappings use the syntax 'mapping(KeyType => ValueType) variableName'.
Question 4: What is the default visibility of state variables in Solidity?
- public
- external
- internal (Correct answer)
- private
Correct answer: internal
State variables default to 'internal' visibility if no modifier is specified.
Question 5: Which Solidity data type stores a sequence of bytes of fixed size up to 32?
- string
- bytes
- bytesN (e.g. bytes32) (Correct answer)
- uint[]
Correct answer: bytesN (e.g. bytes32)
Fixed-size byte arrays like bytes1 through bytes32 store a fixed number of bytes efficiently.
Question 6: What happens when you call 'require(false)' in a Solidity function?
- The contract is destroyed
- The transaction reverts and remaining gas is refunded (Correct answer)
- The function returns false
- An event is emitted
Correct answer: The transaction reverts and remaining gas is refunded
require(false) causes the transaction to revert, undoing state changes and refunding unused gas.
Question 7: In Solidity, what is the type of 'msg.value'?
- int256
- uint256 (Correct answer)
- address
- bytes32
Correct answer: uint256
msg.value is a uint256 representing the amount of Wei sent with the current call.
Which keyword is used to declare a function that cannot modify contract state in Solidity?