Challenge - 5 Problems
Multiple Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
In Solidity, the order of inheritance matters for super calls.
✗ Incorrect
In Solidity, when multiple inheritance is used, the order of base contracts in the inheritance list determines which parent contract's method is called by super. Here, ParentA is listed first, so super.getValue() calls ParentA's version, returning "A".
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Remember Solidity uses C3 linearization for inheritance order.
✗ Incorrect
Solidity uses C3 linearization to resolve inheritance. The order is Child -> B -> C -> A. Calling super.getValue() in Child calls B's getValue(), which calls C's getValue(), which calls A's getValue(). The concatenation results in "BCA".
🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Check the override keyword and which base contracts are specified.
✗ Incorrect
In Solidity, when overriding a function from multiple base contracts, you must specify all base contracts in the override keyword. Here, override() is missing the base contracts X and Y, causing a compilation error.
📝 Syntax
advanced2: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) { } }
Attempts:
2 left
💡 Hint
Parent constructors are called in the inheritance list after the constructor signature.
✗ Incorrect
In Solidity, parent constructors are called in the constructor header using the syntax ParentName(args). Option D correctly calls both A and B constructors. Options B and D try to call constructors inside the body, which is invalid. Option D tries to call super with two arguments, which is invalid.
🚀 Application
expert3: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(); } }
Attempts:
2 left
💡 Hint
Check how Solidity handles state variables with the same name in multiple inheritance.
✗ Incorrect
Solidity does not allow multiple base contracts to have state variables with the same name because it causes storage layout conflicts. This code will fail to compile due to duplicate 'counter' variables in CounterA and CounterB.