A constant is a value that is set once and cannot be changed during the execution of a smart contract. It helps keep data safe and predictable.
Click to reveal answer
beginner
What does 'immutable' mean in the context of blockchain variables?
Immutable means the variable can be assigned only once during contract creation and cannot be changed afterward, ensuring fixed data after deployment.
Click to reveal answer
intermediate
Why use constants and immutables in smart contracts?
They save gas (transaction cost) and increase security by preventing accidental or malicious changes to important values.
Click to reveal answer
intermediate
How do constants differ from immutables in blockchain code?
Constants are fixed at compile time and cannot be changed ever. Immutables are set once during contract deployment and then stay fixed.
Click to reveal answer
beginner
Example: What happens if you try to change a constant value in a smart contract?
The code will not compile or will throw an error because constants cannot be modified after they are set.
Click to reveal answer
Which of the following is true about constants in blockchain smart contracts?
AThey are only used for temporary values.
BThey can be changed anytime during contract execution.
CThey are set during contract deployment and can be changed later.
DThey are set once and cannot be changed ever.
✗ Incorrect
Constants are fixed values set at compile time and cannot be changed.
What is the main difference between constants and immutables?
AImmutables can be changed anytime; constants cannot.
BConstants are cheaper to use than immutables.
CConstants are set at compile time; immutables are set once during deployment.
DThere is no difference.
✗ Incorrect
Constants are fixed at compile time, while immutables are assigned once during contract deployment.
Why do constants and immutables save gas in smart contracts?
ABecause their values are stored in cheaper ways and not changed.
BBecause they allow unlimited changes.
CBecause they increase contract size.
DBecause they require more computation.
✗ Incorrect
Fixed values reduce storage and computation costs, saving gas.
When can an immutable variable be assigned a value?
AOnly once during contract deployment.
BMultiple times during contract execution.
COnly after contract deployment.
DNever assigned a value.
✗ Incorrect
Immutables are assigned once during deployment and then cannot be changed.
What happens if you try to modify a constant after deployment?
AThe contract crashes at runtime.
BThe contract will not compile or will throw an error.
CThe contract ignores the change silently.
DThe constant value changes successfully.
✗ Incorrect
Constants cannot be modified; attempting to do so causes compile errors.
Explain the difference between constants and immutables in blockchain smart contracts.
Think about when each value is assigned and if it can change later.
You got /4 concepts.
Why is it important to use constants and immutables in smart contract development?
Consider how fixed values help protect your contract and save costs.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between constant and immutable variables in a blockchain smart contract?
easy
A. Both constant and immutable variables can be changed after deployment.
B. constant variables can be changed anytime, immutable variables cannot be changed.
C. immutable variables are fixed at compile time, constant variables are set during contract creation.
D. constant variables are fixed at compile time, immutable variables are set once during contract creation.
Solution
Step 1: Understand constant variables
constant variables are assigned a value at compile time and cannot be changed later.
Step 2: Understand immutable variables
immutable variables are assigned once during contract creation (deployment) and then remain fixed.
Final Answer:
constant fixed at compile time, immutable set once at creation -> Option D
Quick Check:
constant = compile time, immutable = creation time [OK]
Hint: Constant fixed at compile, immutable set once at deployment [OK]
Common Mistakes:
Thinking immutable can be changed after deployment
Confusing when constants are assigned
Assuming both can be modified anytime
2. Which of the following is the correct way to declare a constant integer named MAX_SUPPLY with value 1000 in Solidity?
easy
A. constant uint MAX_SUPPLY = 1000;
B. uint constant MAX_SUPPLY = 1000;
C. uint MAX_SUPPLY constant = 1000;
D. constant MAX_SUPPLY uint = 1000;
Solution
Step 1: Recall Solidity constant syntax
In Solidity, the type comes first, then the keyword constant, then the variable name.
Step 2: Check each option
uint constant MAX_SUPPLY = 1000; matches the correct syntax: uint constant MAX_SUPPLY = 1000;. Others have wrong order.
Final Answer:
uint constant MAX_SUPPLY = 1000; -> Option B
Quick Check:
Type + constant + name = correct order [OK]
Hint: Type first, then constant keyword, then variable name [OK]
Common Mistakes:
Placing constant before type
Putting constant after variable name
Missing semicolon at end
3. Consider the Solidity contract snippet:
contract Example {
uint public immutable creationTime;
constructor() {
creationTime = block.timestamp;
}
}
What will creationTime hold after deployment?
medium
A. The timestamp when the contract was deployed
B. The timestamp when the contract was compiled
C. Zero, because it is not initialized
D. It will cause a compilation error
Solution
Step 1: Understand immutable assignment
immutable variables can be assigned once during contract creation, here in the constructor.
Step 2: Analyze the assignment
creationTime = block.timestamp; sets it to the current block timestamp at deployment.
Final Answer:
The timestamp when the contract was deployed -> Option A
Quick Check:
Immutable set at deployment time [OK]
Hint: Immutable set once in constructor at deployment [OK]
Common Mistakes:
Thinking it holds compile time value
Assuming default zero value
Confusing immutable with constant
4. What is wrong with this Solidity code snippet?
contract Test {
uint public immutable value;
function setValue(uint _val) public {
value = _val;
}
}
medium
A. You cannot assign to an immutable variable outside the constructor.
B. The variable value should be declared as constant.
C. The function setValue should be marked as view.
D. There is no error; the code is correct.
Solution
Step 1: Recall rules for immutable variables
immutable variables can only be assigned once during contract creation, typically in the constructor.
Step 2: Check assignment location
Here, value is assigned inside a public function, which is not allowed.
Final Answer:
Cannot assign immutable outside constructor -> Option A
Quick Check:
Immutable assigned only once in constructor [OK]
Hint: Immutable only set in constructor, not in other functions [OK]
Common Mistakes:
Trying to assign immutable in regular functions
Confusing constant and immutable rules
Thinking public functions can set immutable
5. You want to store a fixed token symbol in your smart contract that never changes and a deployment timestamp that is set once at deployment. Which declarations are correct?