0
0
Blockchain / Solidityprogramming~10 mins

Contract inheritance 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 contract that inherits from another contract.

Blockchain / Solidity
contract Base {
    uint public value;
}

contract Derived is [1] {
}
Drag options to blanks, or click blank then click option'
AContract
BDerived
CBase
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong contract name after is.
Forgetting to use the is keyword.
2fill in blank
medium

Complete the code to call the base contract constructor with a value.

Blockchain / Solidity
contract Base {
    uint public value;
    constructor(uint _value) {
        value = _value;
    }
}

contract Derived is Base {
    constructor() Base([1]) {}
}
Drag options to blanks, or click blank then click option'
A10
Bvalue
CBase
D_value
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name instead of a value.
Not calling the base constructor at all.
3fill in blank
hard

Fix the error in the code by completing the inheritance syntax correctly.

Blockchain / Solidity
contract A {
    function greet() public pure returns (string memory) {
        return "Hello from A";
    }
}

contract B [1] A {
}
Drag options to blanks, or click blank then click option'
Ais
Bextends
Cimplements
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using extends or inherits which are not valid in Solidity.
Forgetting the keyword entirely.
4fill in blank
hard

Fill both blanks to override a function from the base contract in the derived contract.

Blockchain / Solidity
contract Base {
    function greet() public pure virtual returns (string memory) {
        return "Hello from Base";
    }
}

contract Derived is Base {
    function greet() public pure override returns (string memory) {
        return [1];
    }
}

// Use the function to get the greeting message
// string memory message = [2];
Drag options to blanks, or click blank then click option'
A"Hello from Derived"
B"Hello from Base"
CDerived().greet()
DBase().greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the base message instead of the new one.
Calling the base contract's function instead of the derived one.
5fill in blank
hard

Fill all three blanks to create a contract that inherits from two contracts and calls both base constructors.

Blockchain / Solidity
contract A {
    uint public a;
    constructor(uint _a) {
        a = _a;
    }
}

contract B {
    uint public b;
    constructor(uint _b) {
        b = _b;
    }
}

contract C is [1], [2] {
    constructor() A(1) [3](2) {}
}
Drag options to blanks, or click blank then click option'
AA
BB
CA()
DB()
Attempts:
3 left
💡 Hint
Common Mistakes
Not listing both base contracts in inheritance.
Not calling both base constructors properly.