Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the role of constructor

    A constructor runs only once when the contract is deployed to set initial values.
  2. Step 2: Compare with other functions

    Other functions run multiple times; constructor is special for initialization only.
  3. Final Answer:

    To set initial values when the contract is deployed -> Option C
  4. 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?

contract MyContract {
    ? {
        // initialization code
    }
}
easy
A. function constructor() public
B. constructor() public
C. def constructor()
D. init() public

Solution

  1. Step 1: Recall Solidity constructor syntax

    In Solidity, the constructor is declared with the keyword constructor() without the function keyword.
  2. 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.
  3. Final Answer:

    constructor() public -> Option B
  4. 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

  1. Step 1: Understand constructor parameter usage

    The constructor takes _value and assigns it to the state variable value.
  2. Step 2: Check deployment argument effect

    Since the contract is deployed with _value = 42, value will be set to 42.
  3. Final Answer:

    42 -> Option A
  4. 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

  1. Step 1: Check Solidity syntax rules

    Each statement must end with a semicolon ;. The line x = 10 is missing a semicolon.
  2. Step 2: Verify constructor naming

    Since Solidity 0.4.22+, constructors use the constructor() keyword, so naming is correct.
  3. Final Answer:

    Missing semicolon after x = 10 -> Option D
  4. 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

  1. Step 1: Understand owner initialization best practice

    Setting owner = msg.sender in the constructor assigns the deployer's address as owner once.
  2. 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.
  3. Final Answer:

    constructor() { owner = msg.sender; } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard