CBCP Smart Contracts and dApps 2 — Questions and Answers
Question 1: Which vulnerability allows an attacker to repeatedly call a function before the first execution finishes, draining a contract's funds?
- Integer overflow
- Reentrancy attack (Correct answer)
- Front-running
- Denial of service
Correct answer: Reentrancy attack
Reentrancy attacks exploit the ability to re-enter a function before its state is updated, as famously demonstrated by The DAO hack in 2016.
Question 2: What Solidity design pattern places the 'check-effects-interactions' principle to prevent reentrancy?
- Update state variables after external calls
- Update state variables before external calls (Correct answer)
- Use delegatecall for all transfers
- Avoid using modifiers
Correct answer: Update state variables before external calls
The checks-effects-interactions pattern requires updating contract state before making any external calls to prevent reentrancy.
Question 3: In Ethereum, what does the 'fallback' function do when a contract receives Ether with no matching function signature?
- It reverts the transaction automatically
- It executes as the default handler for unmatched calls or plain Ether transfers (Correct answer)
- It emits an event and returns the Ether
- It calls the constructor again
Correct answer: It executes as the default handler for unmatched calls or plain Ether transfers
The fallback function is invoked when no other function matches the call data, or when Ether is sent without any call data.
Question 4: Which ERC standard is commonly used for non-fungible tokens (NFTs) on Ethereum?
- ERC-20
- ERC-721 (Correct answer)
- ERC-1155
- ERC-777
Correct answer: ERC-721
ERC-721 defines the standard interface for non-fungible tokens, where each token has a unique identifier and cannot be exchanged 1:1 with another.
Question 5: What is the primary role of an oracle in a smart contract ecosystem?
- To deploy contracts to the blockchain
- To provide off-chain real-world data to on-chain smart contracts (Correct answer)
- To validate transactions in the mempool
- To compile Solidity code into bytecode
Correct answer: To provide off-chain real-world data to on-chain smart contracts
Oracles act as bridges that fetch and verify external data (prices, weather, sports scores) and deliver it to smart contracts that cannot access the internet directly.
Question 6: What is a 'proxy pattern' in smart contract architecture primarily used for?
- Reducing gas costs by caching results
- Enabling upgradeable smart contracts by separating logic from storage (Correct answer)
- Encrypting contract storage on-chain
- Batching multiple transactions into one
Correct answer: Enabling upgradeable smart contracts by separating logic from storage
The proxy pattern separates a contract's storage proxy from its logic implementation, allowing the logic to be upgraded without changing the contract address.
Question 7: In Solidity, what keyword restricts a function so it cannot modify the contract's state?
- pure
- view (Correct answer)
- static
- immutable
Correct answer: view
The 'view' keyword declares that a function reads state variables but does not modify them, which allows it to be called without a transaction.
Which vulnerability allows an attacker to repeatedly call a function before the first execution finishes, draining a contract's funds?