0
0
Blockchain / Solidityprogramming~20 mins

Constants and immutables in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Immutable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A(0, 200)
B(100, 200)
C(100, 0)
D(200, 100)
Attempts:
2 left
💡 Hint
Remember that constant variables are fixed at compile time, and immutable variables are set once during construction.
🧠 Conceptual
intermediate
1:30remaining
Understanding immutables in smart contracts
Which statement about immutable variables in Solidity is correct?
AImmutable variables can be changed multiple times after contract deployment.
BImmutable variables must be assigned a value at declaration and cannot be changed later.
CImmutable variables can be assigned once during contract construction and then cannot be changed.
DImmutable variables behave exactly like constant variables and are replaced at compile time.
Attempts:
2 left
💡 Hint
Think about when the value of an immutable variable is set.
🔧 Debug
advanced
2: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;
    }
}
ATypeError: Constant variables must be assigned at declaration.
BSyntaxError: Missing semicolon after constant declaration.
CRuntimeError: Cannot assign to constant variable in constructor.
DNo error, code compiles and runs fine.
Attempts:
2 left
💡 Hint
Check where constant variables must be assigned.
Predict Output
advanced
2: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;
    }
}
AReading CONST_VAL costs less gas than reading IMMUTABLE_VAL.
BReading IMMUTABLE_VAL costs less gas than reading CONST_VAL.
CReading CONST_VAL costs gas, but reading IMMUTABLE_VAL is free.
DBoth CONST_VAL and IMMUTABLE_VAL cost the same gas to read.
Attempts:
2 left
💡 Hint
Consider where the values are stored and how the EVM accesses them.
🧠 Conceptual
expert
2:30remaining
Why use immutables instead of constants in Solidity?
Which is the best reason to use immutable variables instead of constant variables in Solidity?
AImmutable variables are stored in memory, constants are stored in storage.
BImmutable variables use less gas than constants when accessed.
CImmutable variables can be changed after deployment, constants cannot.
DImmutable variables allow setting values at runtime during contract deployment, while constants require compile-time values.
Attempts:
2 left
💡 Hint
Think about when you know the value you want to assign.