What is a constructor function in a smart contract?
A constructor function is a special function that runs only once when the smart contract is created. It sets up initial values or settings for the contract.
Click to reveal answer
beginner
When is the constructor function executed?
The constructor function is executed automatically during the deployment of the smart contract, before any other functions can be called.
Click to reveal answer
beginner
Can a smart contract have more than one constructor function?
No, a smart contract can only have one constructor function. It is unique and used to initialize the contract once.
Click to reveal answer
intermediate
How do you define a constructor function in Solidity (Ethereum's language)?
In Solidity, you define a constructor using the keyword 'constructor()' followed by its code block. For example: constructor() { /* initialization code */ }
Click to reveal answer
beginner
Why is the constructor function important in blockchain smart contracts?
It sets the initial state of the contract, like owner address or initial balances, ensuring the contract starts correctly and securely.
Click to reveal answer
When does the constructor function run in a smart contract?
AOnly once during contract deployment
BEvery time a function is called
CWhen the contract is destroyed
DWhen a user sends a transaction
✗ Incorrect
The constructor runs only once when the contract is deployed to set initial values.
How many constructor functions can a smart contract have?
ATwo
BOne
CAs many as needed
DNone
✗ Incorrect
A smart contract can have only one constructor function.
Which keyword is used to define a constructor in Solidity?
Ainit
Bsetup
Cconstructor
Dstart
✗ Incorrect
The keyword 'constructor' defines the constructor function in Solidity.
What is a common use of a constructor function?
AInitialize contract state variables
BDestroy the contract
CSend tokens
DCall other contracts
✗ Incorrect
Constructors usually initialize important variables like owner or balances.
Can the constructor function be called after deployment?
AOnly by the owner
BYes, anytime
COnly if the contract is paused
DNo, it runs only once during deployment
✗ Incorrect
The constructor cannot be called again after deployment.
Explain what a constructor function is and why it is used in blockchain smart contracts.
Think about what happens when you first create a contract.
You got /4 concepts.
Describe how to write a constructor function in Solidity and what it typically contains.
Look for the special function named 'constructor' in Solidity.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of a constructor function in a blockchain smart contract?
easy
A. To execute code every time a function is called
B. To destroy the contract and remove it from the blockchain
C. To set initial values when the contract is deployed
D. To transfer tokens between users
Solution
Step 1: Understand the role of constructor
A constructor runs only once when the contract is deployed to set initial values.
Step 2: Compare with other functions
Other functions run multiple times; constructor is special for initialization only.
Final Answer:
To set initial values when the contract is deployed -> Option C
Quick Check:
Constructor sets initial values = B [OK]
Hint: Constructor runs once at deployment to set starting values [OK]
Common Mistakes:
Thinking constructor runs on every function call
Confusing constructor with regular functions
Believing constructor can be called multiple times
2.
Which of the following is the correct syntax to declare a constructor in Solidity?
In Solidity, the constructor is declared with the keyword constructor() without the function keyword.
Step 2: Check options for correct syntax
constructor() public matches the correct syntax: constructor() public. function constructor() public wrongly uses function constructor(), which is outdated. def constructor() uses Python syntax. init() public uses a wrong name.
Final Answer:
constructor() public -> Option B
Quick Check:
Solidity constructor uses 'constructor()' keyword = C [OK]
Hint: Use 'constructor()' keyword without 'function' in Solidity [OK]
Common Mistakes:
Using 'function constructor()' which is deprecated
Using other function names like 'init()'
Mixing syntax from other languages
3.
What will be the output of the following Solidity contract after deployment?
contract Simple {
uint public value;
constructor(uint _value) {
value = _value;
}
}
Assuming the contract is deployed with _value = 42, what is the value of value?
medium
A. 42
B. Compilation error
C. Undefined
D. 0
Solution
Step 1: Understand constructor parameter usage
The constructor takes _value and assigns it to the state variable value.
Step 2: Check deployment argument effect
Since the contract is deployed with _value = 42, value will be set to 42.
Final Answer:
42 -> Option A
Quick Check:
Constructor sets value to 42 at deployment = D [OK]
Hint: Constructor sets state variables from deployment arguments [OK]
Common Mistakes:
Assuming default zero value instead of passed value
Thinking constructor runs after deployment
Confusing variable names
4.
Identify the error in this Solidity contract constructor:
contract Test {
uint public x;
constructor() {
x = 10
}
}
medium
A. Constructor cannot assign values to variables
B. Constructor name should be Test
C. State variable x is not declared correctly
D. Missing semicolon after x = 10
Solution
Step 1: Check Solidity syntax rules
Each statement must end with a semicolon ;. The line x = 10 is missing a semicolon.
Step 2: Verify constructor naming
Since Solidity 0.4.22+, constructors use the constructor() keyword, so naming is correct.
Final Answer:
Missing semicolon after x = 10 -> Option D
Quick Check:
Statements need semicolons in Solidity = A [OK]
Hint: Always end Solidity statements with semicolon [OK]
Common Mistakes:
Using old constructor naming style
Forgetting semicolons
Thinking constructor can't assign variables
5.
You want to create a smart contract that stores an owner address set only once during deployment and cannot be changed later. Which constructor code snippet correctly achieves this?
contract OwnerContract {
address public owner;
?
}
hard
A. constructor() { owner = msg.sender; }
B. function OwnerContract(address _owner) { owner = _owner; }
C. constructor(address _owner) { owner = _owner; }
D. constructor() { owner = address(0); }
Solution
Step 1: Understand owner initialization best practice
Setting owner = msg.sender in the constructor assigns the deployer's address as owner once.
Step 2: Evaluate options
constructor(address _owner) { owner = _owner; } requires passing owner address manually, which is possible but less secure. function OwnerContract(address _owner) { owner = _owner; } uses old constructor syntax. constructor() { owner = address(0); } sets owner to zero address, which is invalid. constructor() { owner = msg.sender; } correctly sets owner to deployer address.
Final Answer:
constructor() { owner = msg.sender; } -> Option A
Quick Check:
Use msg.sender in constructor to set owner = A [OK]
Hint: Use msg.sender in constructor to set owner securely [OK]
Common Mistakes:
Using old constructor syntax
Setting owner to zero address
Passing owner address manually risking errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.