0
0
Blockchain / Solidityprogramming~20 mins

Solidity compiler optimization in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Solidity Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Effect of optimizer on gas usage

Consider the following Solidity contract compiled with and without optimization enabled. What is the expected difference in gas cost when calling increment()?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Counter {
    uint256 public count;

    function increment() public {
        count += 1;
    }
}
AThere is no difference in gas usage between optimized and non-optimized versions.
BThe optimized version uses more gas because it adds extra checks.
CThe optimized version uses less gas because it reduces redundant instructions.
DThe non-optimized version uses less gas because it has simpler bytecode.
Attempts:
2 left
💡 Hint

Think about how compiler optimization removes unnecessary steps.

🧠 Conceptual
intermediate
2:00remaining
Understanding optimizer runs parameter

What does increasing the runs parameter in Solidity compiler optimization do?

AIt instructs the optimizer to optimize for more frequent function calls, reducing runtime gas cost at the expense of deployment size.
BIt tells the optimizer to focus on reducing deployment size over runtime gas cost.
CIt disables optimization for constructor functions only.
DIt increases the number of compiler warnings during compilation.
Attempts:
2 left
💡 Hint

Think about what 'runs' means in terms of how often functions are called.

🔧 Debug
advanced
2:00remaining
Identify the cause of increased gas after optimization

A developer notices that after enabling optimization, a contract's transfer function uses more gas than before. What is the most likely cause?

Blockchain / Solidity
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;
        }
    }
}
AThe optimizer adds redundant events that increase gas cost.
BThe optimizer removes the <code>unchecked</code> block, adding overflow checks and increasing gas.
CThe optimizer changes storage layout, causing more expensive storage reads.
DThe optimizer inlines the <code>require</code> statement, increasing bytecode size and gas.
Attempts:
2 left
💡 Hint

Consider what unchecked does and how optimization might affect it.

📝 Syntax
advanced
2:00remaining
Which pragma enables optimizer with 200 runs?

Choose the correct Solidity pragma directive to enable optimizer with 200 runs.

A
pragma solidity ^0.8.0; // SPDX-License-Identifier: MIT
// solc optimizer runs=200 enabled=true
Bpragma solidity ^0.8.0; optimizer: { enabled: true, runs: 200 };
Cpragma solidity ^0.8.0; // optimizer runs=200 enabled=true
Dpragma solidity ^0.8.0; // solc optimizer runs=200 enabled=true
Attempts:
2 left
💡 Hint

Remember pragma directives only specify compiler version; optimizer settings are usually in config files or comments.

🚀 Application
expert
3:00remaining
Predict gas savings from optimizer on complex contract

A complex contract has many small functions called frequently. The optimizer is enabled with 1000 runs. Which statement best describes the expected effect?

ABoth deployment size and gas cost per call will decrease significantly.
BDeployment size will decrease significantly, but gas cost per call will increase.
CNeither deployment size nor gas cost per call will change noticeably.
DGas cost per function call will decrease significantly, but deployment size will increase.
Attempts:
2 left
💡 Hint

Think about how high runs affect optimization trade-offs.