π Master Solidity: Must-Know Interview Questions for Ethereum Developers π οΈ (Theory Only)
As blockchain technology evolves, the demand for skilled Ethereum developers, particularly those proficient in Solidity, continues to rise. Whether youβre a developer preparing for an interview or a recruiter evaluating candidates, having a comprehensive list of questions is key. This article compiles over 200 carefully curated interview questions, each of which can be answered in three sentences or less. These questions are grouped into categories, making it easier to assess specific areas of knowledge.
𧩠Basic Solidity Questions
What is Solidity?
π Solidity is a high-level programming language designed for writing smart contracts on the Ethereum blockchain.
How does Ethereum differ from Bitcoin?
β‘ Ethereum is a platform for decentralized applications with its own cryptocurrency, Ether, while Bitcoin is primarily a digital currency with a limited scripting language.
What is a smart contract?
π A smart contract is a self-executing contract with the terms of the agreement directly written into code.
What are the main data types in Solidity?
ποΈ Solidity supports several data types, including uint, int, bool, address, bytes, and string.
How do you declare a variable in Solidity?
βοΈ Variables in Solidity are declared with a data type, followed by a variable name, e.g., uint myVariable
.
What is the difference between public
and private
visibility in Solidity?
π public
functions and variables can be accessed externally, while private
ones can only be accessed within the contract that defines them.
What is an address in Solidity?
π An address in Solidity is a 20-byte value that represents the location of an account or contract on the Ethereum network.
Explain the use of the msg.sender
keyword.
π¬ msg.sender
refers to the address of the account that called the contract's function.
What is the fallback
function in Solidity?
πͺ The fallback
function is a special function that gets executed when a contract is called, but no other function matches the given function signature.
How does Ethereum handle gas fees?
β½ Gas fees in Ethereum are the costs required to perform transactions or execute contracts, measured in βgasβ units and paid in Ether.
π What is the difference between private, internal, public, and external functions?
- Private functions are accessible only within the contract, internal functions are accessible within the contract and derived contracts, public functions are accessible from anywhere, and external functions are accessible only from outside the contract.
π Approximately, how large can a smart contract be?
- A smart contract can be up to 24KB in size.
π What is the difference between create and create2?
create
generates a contract at an address based on the sender and nonce, whilecreate2
allows specifying the contract address deterministically using a salt.
π What is access control and why is it important?
- Access control restricts who can execute certain functions, protecting contracts from unauthorized use and potential vulnerabilities.
β What major change with arithmetic happened with Solidity 0.8.0?
- Solidity 0.8.0 introduced automatic overflow and underflow checks, reverting transactions on arithmetic errors.
π§ What special CALL is required for proxies to work?
- The
delegatecall
function is used for proxies, allowing a contract to execute code from another contract in the context of the caller.
πΈ Prior to EIP-1559, how do you calculate the dollar cost of an Ethereum transaction?
- The cost is calculated as
Gas Price Γ Gas Used Γ ETH Price
.
π² What are the challenges of creating a random number on the blockchain?
- Blockchain data is deterministic and public, making it difficult to generate truly random numbers without potential manipulation.
πΈ What is the difference between transfer and transferFrom in ERC20?
transfer
sends tokens from the caller's account, whiletransferFrom
allows transferring tokens on behalf of another address after approval.
π What hash function does Ethereum primarily use?
- Ethereum primarily uses the Keccak-256 hash function.
π° How much is 1 gwei of Ether?
- 1 gwei equals 0.000000001 Ether.
π° How much is 1 wei of Ether?
- 1 wei equals 0.000000000000000001 Ether.
π What is the difference between tx.origin and msg.sender?
tx.origin
returns the address of the original sender of the transaction, whilemsg.sender
returns the address of the immediate caller.
βοΈ Intermediate Solidity Questions
What is the purpose of the require
function in Solidity?
β
The require
function checks for a condition and reverts the transaction if the condition is not met.
Explain the mapping
data type in Solidity.
π mapping
is a key-value store where keys are hashed and mapped to corresponding values, similar to a hash table.
What are events in Solidity?
π’ Events in Solidity allow contracts to log information that can be accessed by external applications, particularly useful for asynchronous communication.
What is the selfdestruct
function?
π£ selfdestruct
removes a contract from the blockchain and sends all its remaining Ether to a specified address.
What is the difference between memory
and storage
in Solidity?
ποΈ memory
is temporary and used for variables within function execution, while storage
is permanent and used for state variables.
What is inheritance in Solidity?
𧬠Inheritance in Solidity allows a contract to inherit properties and functions from another contract, promoting code reuse.
Explain the concept of a modifier in Solidity.
ποΈ A modifier is a function that is used to alter the behavior of other functions, typically for access control or input validation.
What are libraries in Solidity?
π Libraries are reusable pieces of code that can be called from within contracts but do not have a state or persistent storage.
What is the delegatecall
function?
π delegatecall
allows a contract to execute code from another contract in the context of the caller contract, preserving the original caller's state.
What is the purpose of the constructor
in Solidity?
π§ The constructor
is a special function that is executed only once when a contract is deployed, used for initial setup.
βοΈ How do you write a gas-efficient for loop in Solidity?
- Use fixed-size loops or batch processing with optimized state changes to minimize gas usage and avoid dynamic resizing.
π What is the difference between abi.encode
and abi.encodePacked
?
abi.encode
provides a tightly packed ABI-encoded output, whileabi.encodePacked
produces a more compact but less collision-resistant encoding.
π’ uint8
, uint32
, uint64
, uint128
, uint256
are all valid uint sizes. Are there others?
- No,
uint8
,uint32
,uint64
,uint128
, anduint256
are the only valid sizes; other sizes are not supported.
β οΈ Under what circumstances could abi.encodePacked
create a vulnerability?
abi.encodePacked
can cause collisions if used improperly, leading to vulnerabilities where different inputs produce the same output.
π What is the difference between a cold read and a warm read?
- A cold read accesses data from storage directly, which is more expensive, while a warm read uses cached data from memory, which is cheaper.
β½ What is the effect on gas of making a function payable
?
- Making a function
payable
allows it to receive Ether and may increase gas usage due to the additional checks and storage operations.
π What is a signature replay attack?
- A signature replay attack involves reusing a signed transaction or message on a different network or contract to perform unauthorized actions.
π οΈ What is gas griefing?
- Gas griefing is an attack where a malicious actor sends transactions with high gas fees to prevent other transactions from being processed or to exploit gas limits.
πΈ Describe the three types of storage gas costs.
SSTORE: Changing storage costs more than reading.
SLOAD: Reading storage is relatively cheaper.
Storage refund: Partial refund is given for clearing storage.
β οΈ What danger do ERC777 tokens pose?
- ERC777 tokens can pose risks such as accidental token transfers to unintended contracts due to their hooks and complex features.
π·οΈ How does safeMint
differ from mint
in the OpenZeppelin ERC721 implementation?
safeMint
ensures the recipient is capable of receiving ERC721 tokens, whilemint
does not include this check.
π₯ͺ What is a sandwich attack?
- A sandwich attack involves placing transactions around a target transaction to manipulate its outcome and profit from price changes.
π How large a uint can be packed with an address in one slot?
- A
uint
up touint64
can be packed with anaddress
in one slot.
π What is ERC165 used for?
- ERC165 is used for contract introspection, allowing contracts to query if another contract implements a particular interface.
πΈ What does ERC721A do to reduce mint costs? What is the tradeoff?
- ERC721A batches multiple token mints into a single transaction to reduce gas costs, but it sacrifices some flexibility and adds complexity.
β±οΈ What is TWAP?
- Time-Weighted Average Price (TWAP) is a method of averaging prices over time to smooth out volatility and provide a stable price reference.
π³ What is a fee-on-transfer token?
- A fee-on-transfer token deducts a fee from each transfer, which is either burned or redistributed.
π How does Compound Finance calculate utilization?
- Utilization is calculated as the ratio of borrowed assets to the total supply of assets in a lending pool.
π‘οΈ Advanced Solidity Questions
How does Solidity handle overflows in arithmetic operations?
π’ Before Solidity 0.8.0, overflows were unchecked by default, but after 0.8.0, Solidity throws an error on overflow/underflow.
What is a reentrancy attack?
π‘οΈ A reentrancy attack occurs when a function makes an external call to another contract, allowing the caller to call back into the original function before the first execution is complete, potentially leading to unexpected behavior.
β οΈ What is an ERC20 approval frontrunning attack?
- Itβs an attack where a malicious actor observes an approval transaction and submits a transaction to spend the approved tokens before the legitimate transaction executes.
π What opcode accomplishes address(this).balance
?
- The opcode
BALANCE
is used to retrieve the balance of an address, includingaddress(this).balance
.
π What is an anonymous Solidity event?
- An anonymous event is one without a name, allowing it to be used for debugging or internal tracking without polluting the event logs.
π Under what circumstances can a function receive a mapping as an argument?
- Functions cannot directly receive mappings as arguments, but mappings can be used internally within functions.
π What is an inflation attack in ERC4626?
- An inflation attack involves manipulating the vaultβs protocol to issue excess tokens, diluting the value of existing tokens.
π’ How many arguments can a Solidity function have?
- Solidity functions can have up to 1024 arguments.
ποΈ How many storage slots does uint64[] x = [1,2,3,4,5]
use? Does it differ from memory?
- In storage,
uint64[]
uses one slot for the array length and additional slots for each element, depending on the array size. In memory, the array is contiguous and does not occupy distinct slots, but it is still subject to size limitations.
π What is the difference between an optimistic rollup and a zk-rollup?
- Optimistic rollups assume transactions are valid and only challenge incorrect ones, while zk-rollups use zero-knowledge proofs to verify transactions off-chain and ensure correctness.
ποΈ How does EIP1967 pick the storage slots, how many are there, and what do they represent?
- EIP1967 uses predefined storage slots for the implementation address, admin address, and beacon, with three slots:
0x360894a13ba1a3210667c828492db98e0e7b7e7d
,0x1
, and0x2
.
π Under what circumstances would a smart contract that works on Ethereum not work on Polygon or Optimism? (Assume no dependencies on external contracts)
- Differences in network parameters, gas costs, and execution environments could cause compatibility issues between Ethereum and Layer 2 solutions like Polygon or Optimism.
βοΈ Why is strict inequality comparisons more gas efficient than β€
or β₯
? What extra opcode(s) are added?
- Strict inequality comparisons (e.g.,
>
,<
) are more gas efficient because they do not require additional branching or conditional opcodes that are needed forβ€
orβ₯
.
π οΈ If a proxy calls an implementation, and the implementation self-destructs in the function that gets called, what happens?
- If the implementation self-destructs, the proxy will fail to execute further functions from the destroyed implementation, leading to potential reverts or errors.
π How does Uniswap V3 determine the boundaries of liquidity intervals?
- Uniswap V3 uses fixed ranges within a price curve, allowing liquidity providers to concentrate their liquidity within specified price ranges for greater efficiency.
π What is the risk-free rate?
- The risk-free rate is the theoretical return on an investment with zero risk, often used as a benchmark for comparing investment returns.
π What is the difference between bytes
and bytes1[]
in memory?
bytes
is a dynamically-sized byte array, whilebytes1[]
is a fixed-size array of single-byte elements;bytes
can grow or shrink, whilebytes1[]
has a fixed size.
π₯ If a user calls a proxy that makes a delegatecall
to A, and A makes a regular call to B, from A's perspective, who is msg.sender
? From B's perspective, who is msg.sender
? From the proxy's perspective, who is msg.sender
?
- From Aβs perspective,
msg.sender
is the proxy contract. From B's perspective,msg.sender
is still the proxy contract. From the proxy's perspective,msg.sender
is the user who initiated the transaction.
π’ Why do a significant number of contract bytecodes begin with 6080604052
? What does that bytecode sequence do?
- This bytecode sequence is a common initialization pattern for Ethereum contracts, setting up the runtime environment and ensuring proper contract setup.
These questions serve as a valuable resource for both interview preparation and candidate evaluation, ensuring that essential aspects of Solidity and Ethereum development are thoroughly covered. Happy coding and good luck with your interviews!
Subscribe to my newsletter
Read articles from Magda Jankowska directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Magda Jankowska
Magda Jankowska
Security Researcher for Web3 and Dark Web Bug hunter Ethical Hacker