Challenge - 5 Problems
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Solidity contract's constructor?
Consider the following Solidity contract. What will be the value of
owner after deployment?Blockchain / Solidity
pragma solidity ^0.8.0;
contract MyContract {
address public owner;
constructor() {
owner = msg.sender;
}
}Attempts:
2 left
💡 Hint
Remember that
msg.sender in the constructor is the account deploying the contract.✗ Incorrect
In Solidity, the constructor runs once when the contract is deployed. The
msg.sender during deployment is the address that creates the contract, so owner is set to that address.❓ Predict Output
intermediate2:00remaining
What happens if a Solidity contract has multiple constructors?
Examine this Solidity contract snippet. What will happen when compiling?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
constructor() {
// First constructor
}
constructor(uint x) {
// Second constructor
}
}Attempts:
2 left
💡 Hint
Solidity only supports one constructor per contract.
✗ Incorrect
Solidity contracts can have only one constructor. Defining multiple constructors causes a compilation error.
🔧 Debug
advanced2:00remaining
Why does this Solidity contract fail to set the owner in the constructor?
Look at this contract. Why is
owner still zero after deployment?Blockchain / Solidity
pragma solidity ^0.8.0;
contract Broken {
address public owner;
function Broken() public {
owner = msg.sender;
}
}Attempts:
2 left
💡 Hint
In Solidity versions 0.7.0 and later, constructors use the
constructor keyword.✗ Incorrect
Before Solidity 0.7.0, constructors were functions named after the contract. From 0.7.0 onwards, the
constructor keyword must be used. Here, Broken() is a normal function, not a constructor, so owner remains unset.🧠 Conceptual
advanced1:30remaining
What is the purpose of a constructor in a smart contract?
Choose the best description of what a constructor does in a smart contract.
Attempts:
2 left
💡 Hint
Think about what happens only once when a contract is created.
✗ Incorrect
A constructor runs only once during deployment to set initial values or perform setup tasks.
❓ Predict Output
expert2:30remaining
What is the output of this Solidity contract with inheritance and constructors?
Given these contracts, what will be the value of
value in Child after deployment?Blockchain / Solidity
pragma solidity ^0.8.0; contract Parent { uint public value; constructor(uint _value) { value = _value; } } contract Child is Parent { constructor() Parent(42) { value = value + 1; } }
Attempts:
2 left
💡 Hint
The child constructor calls the parent constructor with 42, then adds 1.
✗ Incorrect
The
Parent constructor sets value to 42. Then the Child constructor adds 1, so final value is 43.