0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Contract Inheritance
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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";
    }
}
A"Parent"
B"Child"
CCompilation error due to missing override
D"Undefined"
Attempts:
2 left
💡 Hint
The child contract overrides the parent's function.
Predict Output
intermediate
2: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();
    }
}
ACompilation error due to ambiguous inheritance
B"B"
C"C"
D"A"
Attempts:
2 left
💡 Hint
In Solidity, the right-most base contract's function is called by super.
🔧 Debug
advanced
2: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();
    }
}
ARuntime error: super.foo() causes infinite recursion
BCompilation error: super.foo() call is invalid
CNo compilation error; code compiles successfully
DCompilation error: function foo must be marked virtual in Derived
Attempts:
2 left
💡 Hint
Check if the overridden function in Derived allows further overrides.
📝 Syntax
advanced
2: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
    }
}
Aconstructor(uint _a, uint _b) A(_a) B(_b) {}
Bconstructor(uint _a, uint _b) { A(_a); B(_b); }
Cconstructor(uint _a, uint _b) : A(_a), B(_b) {}
Dconstructor(uint _a, uint _b) super(_a, _b) {}
Attempts:
2 left
💡 Hint
Base constructors are called in the inheritance list after the constructor signature.
🚀 Application
expert
3: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;
    }
}
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Remember the order of constructor calls in multiple inheritance and that the most derived constructor runs last.