Challenge - 5 Problems
Master of Contract Inheritance
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inherited contract function call
What is the output when calling
getValue() from contract Child?Blockchain / Solidity
pragma solidity ^0.8.0; contract Parent { function getValue() public pure virtual returns (string memory) { return "Parent"; } } contract Child is Parent { function getValue() public pure override returns (string memory) { return "Child"; } }
Attempts:
2 left
💡 Hint
The child contract overrides the parent's function.
✗ Incorrect
The
Child contract overrides getValue() and returns "Child". So calling getValue() on Child returns "Child".❓ Predict Output
intermediate2:00remaining
Multiple inheritance function resolution
What is the output of
getName() when called from contract GrandChild?Blockchain / Solidity
pragma solidity ^0.8.0; contract A { function getName() public pure virtual returns (string memory) { return "A"; } } contract B is A { function getName() public pure virtual override returns (string memory) { return "B"; } } contract C is A { function getName() public pure virtual override returns (string memory) { return "C"; } } contract GrandChild is B, C { function getName() public pure override(B, C) returns (string memory) { return super.getName(); } }
Attempts:
2 left
💡 Hint
In Solidity, the right-most base contract's function is called by super.
✗ Incorrect
In multiple inheritance,
super calls the function from the right-most base contract in the inheritance list. Here, C is right-most, so getName() returns "C".🔧 Debug
advanced2:00remaining
Identify the error in contract inheritance
What error will this Solidity code produce when compiled?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Base { function foo() public pure returns (string memory) { return "Base"; } } contract Derived is Base { function foo() public pure override returns (string memory) { return "Derived"; } } contract Final is Derived { function foo() public pure override returns (string memory) { return super.foo(); } }
Attempts:
2 left
💡 Hint
Check if the overridden function in Derived allows further overrides.
✗ Incorrect
The function
foo() in Derived overrides Base but is not marked virtual. Without virtual, Final cannot override it, causing a compilation error.📝 Syntax
advanced2:00remaining
Correct syntax for multiple inheritance constructor calls
Which option correctly calls constructors of both base contracts
A and B in contract C?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 C is A, B { constructor(uint _a, uint _b) /* ??? */ { // constructor body } }
Attempts:
2 left
💡 Hint
Base constructors are called in the inheritance list after the constructor signature.
✗ Incorrect
In Solidity, base constructors are called using the syntax
constructor(...) Base1(args) Base2(args) {}. Option A uses this correct syntax.🚀 Application
expert3:00remaining
Determine final state variable value after complex inheritance
Given the contracts below, what is the value of
value in contract Final after deployment?Blockchain / Solidity
pragma solidity ^0.8.0; contract X { uint public value = 1; } contract Y is X { constructor() { value = 2; } } contract Z is X { constructor() { value = 3; } } contract Final is Y, Z { constructor() { value = 4; } }
Attempts:
2 left
💡 Hint
Remember the order of constructor calls in multiple inheritance and that the most derived constructor runs last.
✗ Incorrect
Constructors run in the order of inheritance: X, then Y, then Z, then Final. Each constructor sets
value. The last assignment is in Final's constructor, setting value to 4.