Complete the code to declare a fixed-size array to save gas on L2.
uint256[[1]] public fixedArray;Using a fixed-size array like uint256[10] helps save gas on L2 by avoiding dynamic array overhead.
Complete the code to use calldata for function parameters to save gas on L2.
function processData(string[1] data) external {}memory which copies data and costs more gas.storage which is not allowed for function parameters.Using calldata for external function parameters saves gas because it avoids copying data into memory.
Fix the error in the code to optimize gas by packing variables.
contract GasSaver {
uint128 public a;
uint[1] public b;
}uint256 which wastes storage slots.Using uint128 for both variables allows Solidity to pack them into one storage slot, saving gas.
Fill both blanks to optimize gas by using unchecked math and short-circuiting.
function increment(uint256 x) public pure returns (uint256) {
uint256 y = unchecked { x [1] 1 };
return y [2] 0 ? y : 0;
}Using unchecked with + avoids overflow checks, saving gas. The comparison y > 0 short-circuits unnecessary operations.
Fill all three blanks to optimize gas by using short-circuit evaluation, minimal storage writes, and efficient loops.
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; }
Using i < data.length avoids out-of-bounds errors. Checking data[i] > 0 skips zero values to save gas. Using += efficiently accumulates the sum.