0
0
Blockchain / Solidityprogramming~10 mins

Constructor function in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a constructor in Solidity.

Blockchain / Solidity
contract MyContract {
    [1]() {
        // constructor code
    }
}
Drag options to blanks, or click blank then click option'
Aconstructor
Bfunction
Cinit
Dsetup
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'function' instead of 'constructor' to declare the constructor.
Naming the constructor like a regular function.
2fill in blank
medium

Complete the code to initialize the state variable in the constructor.

Blockchain / Solidity
contract MyContract {
    uint public value;
    constructor(uint _value) {
        value = [1];
    }
}
Drag options to blanks, or click blank then click option'
Avalue
B_val
C_value
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared as a parameter.
Assigning the state variable to itself.
3fill in blank
hard

Fix the error in the constructor declaration for Solidity version 0.8+.

Blockchain / Solidity
contract MyContract {
    uint public value;
    [1](uint _value) {
        value = _value;
    }
}
Drag options to blanks, or click blank then click option'
Aconstructor
Bfunction MyContract
Cfunction constructor
DMyContract
Attempts:
3 left
💡 Hint
Common Mistakes
Using the contract name as constructor function name.
Including the 'function' keyword with constructor.
4fill in blank
hard

Fill both blanks to create a constructor that sets two state variables.

Blockchain / Solidity
contract MyContract {
    string public name;
    uint public age;
    constructor(string memory [1], uint [2]) {
        name = _name;
        age = _age;
    }
}
Drag options to blanks, or click blank then click option'
A_name
Bname
C_age
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using state variable names as parameter names causing confusion.
Not matching parameter names with those used in the constructor body.
5fill in blank
hard

Fill all three blanks to create a constructor that initializes three state variables.

Blockchain / Solidity
contract MyContract {
    string public title;
    address public owner;
    uint public amount;
    constructor(string memory [1], address [2], uint [3]) {
        title = _title;
        owner = _owner;
        amount = _amount;
    }
}
Drag options to blanks, or click blank then click option'
A_title
B_owner
C_amount
Downer
Attempts:
3 left
💡 Hint
Common Mistakes
Using state variable names instead of parameter names in the constructor signature.
Mismatching parameter names and assignments.