Solidity Basic Solidity (Web Dev) 3 — Questions and Answers
Question 1: Which Solidity construct is used to emit a log that can be listened to off-chain?
- emit EventName() (Correct answer)
- log EventName()
- trigger EventName()
- fire EventName()
Correct answer: emit EventName()
The 'emit' keyword is used to trigger an event, which is stored in transaction logs.
Question 2: What is the purpose of the 'constructor' function in a Solidity contract?
- It's called on every function invocation
- It runs once when the contract is deployed (Correct answer)
- It destroys the contract
- It handles incoming Ether
Correct answer: It runs once when the contract is deployed
The constructor runs exactly once at deployment and is used to initialize contract state.
Question 3: Which keyword allows a Solidity contract to inherit from another contract?
- extends
- implements
- is (Correct answer)
- inherits
Correct answer: is
Solidity uses the 'is' keyword for inheritance, e.g., 'contract Child is Parent'.
Question 4: What does the 'storage' keyword indicate when used as a data location in Solidity?
- The variable is stored in calldata
- The variable persists on the blockchain between calls (Correct answer)
- The variable is temporary and cleared after the call
- The variable is read-only
Correct answer: The variable persists on the blockchain between calls
Storage variables are persistent and saved to the blockchain, making reads/writes more expensive.
Question 5: How do you send Ether from a contract to an address in modern Solidity (0.8+)?
- address.transfer(amount)
- address.send(amount)
- payable(address).transfer(amount) (Correct answer)
- address.call{value: amount}()
Correct answer: payable(address).transfer(amount)
In Solidity 0.8+, you must cast to payable before calling .transfer(), e.g., payable(addr).transfer(amount).
Question 6: Which of the following correctly declares a public dynamic array of uint in Solidity?
- uint[] public numbers; (Correct answer)
- uint[dynamic] public numbers;
- array<uint> public numbers;
- uint public numbers[];
Correct answer: uint[] public numbers;
Dynamic arrays in Solidity are declared with empty brackets: 'uint[] public numbers'.
Question 7: What is the 'fallback' function used for in Solidity?
- To handle failed transactions
- To execute when a contract receives a call with no matching function selector (Correct answer)
- To initialize state variables
- To destroy the contract
Correct answer: To execute when a contract receives a call with no matching function selector
The fallback function is invoked when a call doesn't match any declared function signature.
Which Solidity construct is used to emit a log that can be listened to off-chain?