Ethereum Developer Solidity Smart Contract Development 1 β Questions and Answers
Question 1: Which Solidity keyword is used to declare a function that cannot modify contract state?
- view (Correct answer)
- pure
- constant
- immutable
Correct answer: view
The `view` keyword declares a function that can read but not modify state variables.
Question 2: What is the purpose of the `payable` keyword in a Solidity function?
- It allows the function to receive Ether (Correct answer)
- It makes the function free to call
- It locks the function from being overridden
- It enables the function to return Ether
Correct answer: It allows the function to receive Ether
The `payable` keyword allows a function to receive Ether as part of a transaction.
Question 3: Which Solidity data type is used to store Ethereum addresses?
- address (Correct answer)
- bytes20
- uint160
- string
Correct answer: address
The `address` type is a 20-byte value specifically designed to hold Ethereum addresses.
Question 4: What does the `require` statement do in Solidity when its condition is false?
- Reverts the transaction and refunds unused gas (Correct answer)
- Emits an error event
- Pauses the contract
- Returns a false boolean
Correct answer: Reverts the transaction and refunds unused gas
When `require` fails, it reverts all state changes and refunds the remaining gas to the caller.
Question 5: In Solidity, what is a `mapping`?
- A hash table that maps keys to values (Correct answer)
- An ordered array of key-value pairs
- A linked list data structure
- A pointer to a storage slot
Correct answer: A hash table that maps keys to values
A `mapping` in Solidity is a hash table that maps keys of one type to values of another type.
Question 6: Which visibility specifier restricts a function to be called only within the contract itself?
- private (Correct answer)
- internal
- external
- public
Correct answer: private
The `private` visibility means the function can only be called from within the same contract, not from derived contracts.
Which Solidity keyword is used to declare a function that cannot modify contract state?