Challenge - 5 Problems
Immutable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of constant and immutable variables in Solidity
What is the output of the following Solidity contract when calling
getValues()?Blockchain / Solidity
pragma solidity ^0.8.0; contract Test { uint256 public constant CONST_VAL = 100; uint256 public immutable IMMUTABLE_VAL; constructor(uint256 _val) { IMMUTABLE_VAL = _val; } function getValues() public view returns (uint256, uint256) { return (CONST_VAL, IMMUTABLE_VAL); } }
Attempts:
2 left
💡 Hint
Remember that constant variables are fixed at compile time, and immutable variables are set once during construction.
✗ Incorrect
The constant variable CONST_VAL is set to 100 at compile time. The immutable variable IMMUTABLE_VAL is set once in the constructor to the passed value (200). So the function returns (100, 200).
🧠 Conceptual
intermediate1:30remaining
Understanding immutables in smart contracts
Which statement about immutable variables in Solidity is correct?
Attempts:
2 left
💡 Hint
Think about when the value of an immutable variable is set.
✗ Incorrect
Immutable variables are assigned once during the contract's constructor execution and cannot be changed afterward. This allows flexibility compared to constants, which must be assigned at compile time.
🔧 Debug
advanced2:00remaining
Identify the error with constant variable assignment
What error will the Solidity compiler produce for this code snippet?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Demo { uint256 public constant MY_CONST; constructor() { MY_CONST = 10; } }
Attempts:
2 left
💡 Hint
Check where constant variables must be assigned.
✗ Incorrect
Constant variables must be assigned a value where they are declared. Assigning them in the constructor causes a compile-time error.
❓ Predict Output
advanced2:00remaining
Gas cost difference between constant and immutable
Given the following Solidity contract, which statement about gas cost when accessing
CONST_VAL and IMMUTABLE_VAL is true?Blockchain / Solidity
pragma solidity ^0.8.0; contract GasTest { uint256 public constant CONST_VAL = 123; uint256 public immutable IMMUTABLE_VAL; constructor() { IMMUTABLE_VAL = 456; } function readConst() public pure returns (uint256) { return CONST_VAL; } function readImmutable() public view returns (uint256) { return IMMUTABLE_VAL; } }
Attempts:
2 left
💡 Hint
Consider where the values are stored and how the EVM accesses them.
✗ Incorrect
Constant variables are replaced by their value at compile time and do not require storage access, so reading them costs less gas. Immutable variables are stored in storage slots and require a storage read, which costs more gas.
🧠 Conceptual
expert2:30remaining
Why use immutables instead of constants in Solidity?
Which is the best reason to use immutable variables instead of constant variables in Solidity?
Attempts:
2 left
💡 Hint
Think about when you know the value you want to assign.
✗ Incorrect
Immutable variables let you assign values during contract deployment (constructor), which is useful when the value depends on deployment parameters. Constants must have fixed values known at compile time.