0
0
Blockchain / Solidityprogramming~10 mins

Gas optimization for L2 in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a fixed-size array to save gas on L2.

Blockchain / Solidity
uint256[[1]] public fixedArray;
Drag options to blanks, or click blank then click option'
A0
B10
Cdynamic
Duint256
Attempts:
3 left
💡 Hint
Common Mistakes
Using dynamic arrays which cost more gas.
Setting size to zero which is invalid.
2fill in blank
medium

Complete the code to use calldata for function parameters to save gas on L2.

Blockchain / Solidity
function processData(string[1] data) external {}
Drag options to blanks, or click blank then click option'
A memory
B storage
C calldata
D public
Attempts:
3 left
💡 Hint
Common Mistakes
Using memory which copies data and costs more gas.
Using storage which is not allowed for function parameters.
3fill in blank
hard

Fix the error in the code to optimize gas by packing variables.

Blockchain / Solidity
contract GasSaver {
    uint128 public a;
    uint[1] public b;
}
Drag options to blanks, or click blank then click option'
A128
B256
C64
D32
Attempts:
3 left
💡 Hint
Common Mistakes
Using uint256 which wastes storage slots.
Using different sizes that prevent packing.
4fill in blank
hard

Fill both blanks to optimize gas by using unchecked math and short-circuiting.

Blockchain / Solidity
function increment(uint256 x) public pure returns (uint256) {
    uint256 y = unchecked { x [1] 1 };
    return y [2] 0 ? y : 0;
}
Drag options to blanks, or click blank then click option'
A+
B>
C<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction which may cause underflow.
Using wrong comparison operators that break logic.
5fill in blank
hard

Fill all three blanks to optimize gas by using short-circuit evaluation, minimal storage writes, and efficient loops.

Blockchain / Solidity
function sum(uint256[] memory data) public pure returns (uint256) {
    uint256 total = 0;
    for (uint256 i = 0; i [1] data.length; i++) {
        if (data[i] [2] 0) {
            total [3]= data[i];
        }
    }
    return total;
}
Drag options to blanks, or click blank then click option'
A<=
B>
C+
D-
E<
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition causing out-of-bounds.
Adding zero values wasting gas.
Using -= instead of += causing wrong results.