Consider the following Solidity contract compiled with and without optimization enabled. What is the expected difference in gas cost when calling increment()?
pragma solidity ^0.8.0; contract Counter { uint256 public count; function increment() public { count += 1; } }
Think about how compiler optimization removes unnecessary steps.
Enabling optimization allows the compiler to reduce redundant instructions and improve bytecode efficiency, which lowers gas consumption when calling functions like increment().
What does increasing the runs parameter in Solidity compiler optimization do?
Think about what 'runs' means in terms of how often functions are called.
The runs parameter guides the optimizer to balance between deployment size and runtime gas cost. Higher runs mean optimizing for frequent calls, which may increase deployment size but reduce gas cost per call.
A developer notices that after enabling optimization, a contract's transfer function uses more gas than before. What is the most likely cause?
pragma solidity ^0.8.0; contract Token { mapping(address => uint256) balances; function transfer(address to, uint256 amount) public { require(balances[msg.sender] >= amount, "Insufficient balance"); unchecked { balances[msg.sender] -= amount; balances[to] += amount; } } }
Consider what unchecked does and how optimization might affect it.
If the optimizer removes the unchecked block, Solidity adds overflow checks by default, which increases gas cost during arithmetic operations.
Choose the correct Solidity pragma directive to enable optimizer with 200 runs.
Remember pragma directives only specify compiler version; optimizer settings are usually in config files or comments.
Optimizer settings are not part of the pragma directive but are specified in config files or comments. Option A shows a valid comment format for SPDX and optimizer settings.
A complex contract has many small functions called frequently. The optimizer is enabled with 1000 runs. Which statement best describes the expected effect?
Think about how high runs affect optimization trade-offs.
High runs value tells the optimizer to reduce runtime gas cost for frequently called functions, often increasing deployment size due to inlining and other optimizations.