Prepare for the Blockchain Developer Certification. Master blockchain concepts with flashcards and multiple choice questions. Explanation-based learning for your success!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


Do local variables in Solidity cost gas?

  1. Yes, they are expensive

  2. No, they are created on the stack

  3. Only when they are computed

  4. Only if they are global variables

The correct answer is: No, they are created on the stack

Local variables in Solidity do not cost gas in the way that other variables, such as state variables or storage variables, do because they are stored in the stack, which is a much more efficient area of memory compared to the heap or persistent storage. In Ethereum, the stack is designed for temporary data storage and has a limited size, allowing for quick access and manipulation without incurring the high costs associated with storage in the Ethereum Virtual Machine (EVM). The fact that local variables are created on the stack means that they do not occupy persistent storage and are not subject to the same gas costs that come from modifying state variables. When you allocate local variables, the cost of using them is minimal, primarily involving the gas required for the computation rather than storage fees. This is a fundamental aspect of Solidity's design that allows developers to optimize their contracts and reduce costs by using local variables effectively. While other options might seem plausible, they do not accurately capture the relationship between Solidity's local variables and gas cost. Understanding the distinction between stack and storage is crucial for developers aiming to enhance the efficiency of their smart contracts.