Ethereum Developer Solidity Smart Contract Development 2 — Questions and Answers
Question 1: What is the Solidity `constructor` function used for?
- Initializing contract state when it is deployed (Correct answer)
- Defining the ABI of the contract
- Upgrading contract logic
- Creating new contract instances
Correct answer: Initializing contract state when it is deployed
The `constructor` runs once during contract deployment and is used to set initial state variables.
Question 2: What does the `emit` keyword do in Solidity?
- Fires an event that gets logged on the blockchain (Correct answer)
- Sends Ether to an address
- Calls an external contract function
- Destroys a contract
Correct answer: Fires an event that gets logged on the blockchain
The `emit` keyword triggers an event, recording a log entry in the transaction receipt on the blockchain.
Question 3: Which Solidity keyword marks a state variable as unchangeable after deployment?
- immutable (Correct answer)
- constant
- final
- fixed
Correct answer: immutable
The `immutable` keyword allows a variable to be set in the constructor but never changed afterward.
Question 4: What is a Solidity `modifier` used for?
- Reusing pre- and post-condition logic across multiple functions (Correct answer)
- Changing the return type of a function
- Optimizing gas usage automatically
- Declaring abstract methods
Correct answer: Reusing pre- and post-condition logic across multiple functions
Modifiers wrap function execution with reusable guard logic, like access control checks, using `_;` as a placeholder.
Question 5: What is the difference between `memory` and `storage` in Solidity?
- memory is temporary and wiped after execution; storage persists on-chain (Correct answer)
- memory is cheaper than storage for all operations
- storage holds function parameters; memory holds state variables
- memory is for arrays only; storage is for all types
Correct answer: memory is temporary and wiped after execution; storage persists on-chain
`memory` variables exist only during function execution while `storage` variables persist permanently on the blockchain.
Question 6: Which Solidity function is called when Ether is sent to a contract with no calldata?
- receive() (Correct answer)
- fallback()
- deposit()
- default()
Correct answer: receive()
The `receive()` function handles plain Ether transfers with empty calldata, and must be marked `payable`.
What is the Solidity `constructor` function used for?