0
0
Blockchain / Solidityprogramming~20 mins

Multiple inheritance in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple inheritance method call
What is the output of this Solidity contract when calling getValue() on Child?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract ParentA {
    function getValue() public pure virtual returns (string memory) {
        return "A";
    }
}

contract ParentB {
    function getValue() public pure virtual returns (string memory) {
        return "B";
    }
}

contract Child is ParentA, ParentB {
    function getValue() public pure override(ParentA, ParentB) returns (string memory) {
        return super.getValue();
    }
}
ACompilation error due to ambiguous super call
B"B"
C"A"
D"AB"
Attempts:
2 left
💡 Hint
In Solidity, the order of inheritance matters for super calls.
Predict Output
intermediate
2:00remaining
Diamond problem resolution in Solidity
Given these contracts, what will Child.getValue() return?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract A {
    function getValue() public pure virtual returns (string memory) {
        return "A";
    }
}

contract B is A {
    function getValue() public pure virtual override returns (string memory) {
        return string(abi.encodePacked("B", super.getValue()));
    }
}

contract C is A {
    function getValue() public pure virtual override returns (string memory) {
        return string(abi.encodePacked("C", super.getValue()));
    }
}

contract Child is B, C {
    function getValue() public pure override(B, C) returns (string memory) {
        return super.getValue();
    }
}
A"BC"
B"BCA"
CCompilation error due to inheritance order
D"CBA"
Attempts:
2 left
💡 Hint
Remember Solidity uses C3 linearization for inheritance order.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple inheritance override
What error will this Solidity code produce when compiled?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract X {
    function foo() public pure virtual returns (string memory) {
        return "X";
    }
}

contract Y {
    function foo() public pure virtual returns (string memory) {
        return "Y";
    }
}

contract Z is X, Y {
    function foo() public pure override returns (string memory) {
        return super.foo();
    }
}
ACompilation error: function foo must override both X and Y
BRuntime error: super.foo() is ambiguous
CCompilation error: override specifier missing base contracts
DNo error, outputs "X"
Attempts:
2 left
💡 Hint
Check the override keyword and which base contracts are specified.
📝 Syntax
advanced
2:00remaining
Correct syntax for multiple inheritance constructor call
Which option correctly calls constructors of both parents in Solidity?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

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

contract Child is A, B {
    constructor(uint _a, uint _b) A(_a) B(_b) {
    }
}
Aconstructor(uint _a, uint _b) A(_a) { B(_b); }
Bconstructor(uint _a, uint _b) { A(_a); B(_b); }
Cconstructor(uint _a, uint _b) { super(_a, _b); }
Dconstructor(uint _a, uint _b) A(_a) B(_b) {}
Attempts:
2 left
💡 Hint
Parent constructors are called in the inheritance list after the constructor signature.
🚀 Application
expert
3:00remaining
Determine final state after multiple inheritance function calls
Given these contracts, what is the value of counter after calling Child.increment() once?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract CounterA {
    uint public counter;
    function increment() public virtual {
        counter += 1;
    }
}

contract CounterB {
    uint public counter;
    function increment() public virtual {
        counter += 10;
    }
}

contract Child is CounterA, CounterB {
    function increment() public override(CounterA, CounterB) {
        CounterA.increment();
        CounterB.increment();
    }
}
ACompilation error due to duplicate state variables
B11
C1
D10
Attempts:
2 left
💡 Hint
Check how Solidity handles state variables with the same name in multiple inheritance.