0
0
Blockchain / Solidityprogramming~20 mins

Constructor function in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
    }
}
AThe <code>owner</code> will be a random address generated by the constructor.
BThe <code>owner</code> will be the zero address (0x0000000000000000000000000000000000000000).
CThe <code>owner</code> will be the address of the contract itself.
DThe <code>owner</code> will be the address of the account that deployed the contract.
Attempts:
2 left
💡 Hint
Remember that msg.sender in the constructor is the account deploying the contract.
Predict Output
intermediate
2: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
    }
}
AThe contract will compile and use the constructor with parameters by default.
BThe contract will compile and use the constructor with no parameters by default.
CCompilation error: Solidity does not allow multiple constructors.
DThe contract will compile and randomly pick one constructor at deployment.
Attempts:
2 left
💡 Hint
Solidity only supports one constructor per contract.
🔧 Debug
advanced
2: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;
    }
}
AThe function named <code>Broken</code> is not a constructor in Solidity 0.8.0, so it is a normal function and never called automatically.
BThe <code>msg.sender</code> is not available in constructors.
CThe <code>owner</code> variable is private and cannot be set.
DThe contract has a syntax error because the constructor must be external.
Attempts:
2 left
💡 Hint
In Solidity versions 0.7.0 and later, constructors use the constructor keyword.
🧠 Conceptual
advanced
1: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.
AIt initializes the contract's state variables once when the contract is deployed.
BIt runs every time a function is called in the contract.
CIt automatically destroys the contract after deployment.
DIt allows users to send Ether to the contract after deployment.
Attempts:
2 left
💡 Hint
Think about what happens only once when a contract is created.
Predict Output
expert
2: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;
    }
}
A42
B43
C0
DCompilation error due to constructor syntax
Attempts:
2 left
💡 Hint
The child constructor calls the parent constructor with 42, then adds 1.