Solidity Basic Solidity (Web Dev) 5 — Questions and Answers
Question 1: What is the result of integer overflow in Solidity 0.8.0 and later?
- The value wraps around silently
- The transaction reverts automatically (Correct answer)
- The value is capped at the maximum
- A warning is emitted but execution continues
Correct answer: The transaction reverts automatically
Solidity 0.8+ includes built-in overflow/underflow checks that revert the transaction on overflow.
Question 2: Which function selector is called when Ether is sent to a contract with no calldata and no fallback?
- The constructor
- The receive() function (Correct answer)
- The default() function
- No function; the transaction reverts
Correct answer: The receive() function
The receive() function, if defined and marked payable, handles plain Ether transfers with no calldata.
Question 3: In Solidity, what is 'calldata' as a data location?
- A temporary memory area that persists between calls
- Read-only non-persistent data location for external function input parameters (Correct answer)
- Storage that is cleared after the transaction
- An alias for 'memory'
Correct answer: Read-only non-persistent data location for external function input parameters
Calldata is a read-only, non-persistent area where external function arguments are stored, cheaper than memory.
Question 4: What does the Solidity 'interface' keyword enforce that 'abstract contract' does not?
- Interfaces can have state variables; abstract contracts cannot
- Interfaces cannot have any implementation; all functions must be external and unimplemented (Correct answer)
- Interfaces support multiple inheritance; abstract contracts do not
- Interfaces can use the 'modifier' keyword; abstract contracts cannot
Correct answer: Interfaces cannot have any implementation; all functions must be external and unimplemented
Interfaces only allow external function declarations with no implementation, no state variables, and no constructors.
Question 5: What is the gas cost implication of using 'storage' vs 'memory' for arrays in Solidity?
- Memory is always more expensive than storage
- Storage reads/writes cost significantly more gas than memory operations (Correct answer)
- They cost the same amount of gas
- Storage is free; only memory costs gas
Correct answer: Storage reads/writes cost significantly more gas than memory operations
Storage operations (SLOAD/SSTORE) are among the most expensive EVM opcodes, far costlier than memory operations.
Question 6: Which of the following correctly uses a struct in Solidity?
- struct Person { name: string; age: uint; }
- struct Person { string name; uint age; } (Correct answer)
- Person struct { string name; uint age; }
- define struct Person(string name, uint age);
Correct answer: struct Person { string name; uint age; }
Solidity struct syntax places the type before the variable name inside curly braces, without colons.
Question 7: What is the purpose of 'selfdestruct(address)' in Solidity?
- It pauses the contract temporarily
- It deletes the contract bytecode from the blockchain and sends remaining Ether to the given address (Correct answer)
- It resets all state variables to zero
- It transfers ownership of the contract
Correct answer: It deletes the contract bytecode from the blockchain and sends remaining Ether to the given address
selfdestruct removes the contract's bytecode from the blockchain and forcefully sends its Ether balance to the specified address.
What is the result of integer overflow in Solidity 0.8.0 and later?