Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What keyword is used in Solidity to inherit from a parent contract?
easy
A. extends
B. inherits
C. is
D. implements

Solution

  1. Step 1: Understand Solidity inheritance syntax

    In Solidity, the keyword is is used to inherit from a parent contract.
  2. Step 2: Compare with other options

    Keywords like extends, inherits, and implements are not used in Solidity for inheritance.
  3. Final Answer:

    is -> Option C
  4. Quick Check:

    Inheritance keyword = is [OK]
Hint: Remember: Solidity uses 'is' for inheritance [OK]
Common Mistakes:
  • Confusing Solidity with other languages like Java using 'extends'
  • Using 'implements' which is for interfaces
  • Trying 'inherits' which is not a Solidity keyword
2. Which of the following is the correct way to declare a function in a parent contract that can be overridden in Solidity?
easy
A. function foo() public virtual {}
B. function foo() public abstract {}
C. function foo() public override {}
D. function foo() public final {}

Solution

  1. Step 1: Identify the keyword to allow overriding

    In Solidity, to allow a function to be overridden in child contracts, it must be marked virtual.
  2. Step 2: Check other options for correctness

    override is used in child contracts, abstract is for abstract contracts, and final is not a Solidity keyword.
  3. Final Answer:

    function foo() public virtual {} -> Option A
  4. Quick Check:

    Allow override = virtual [OK]
Hint: Use 'virtual' in parent to allow overrides [OK]
Common Mistakes:
  • Using 'override' in parent instead of child
  • Confusing 'abstract' with 'virtual'
  • Using non-existent 'final' keyword
3. What will be the output of the following Solidity code when calling greet() on contract Child?
contract Parent {
    function greet() public virtual returns (string memory) {
        return "Hello from Parent";
    }
}

contract Child is Parent {
    function greet() public override returns (string memory) {
        return "Hello from Child";
    }
}
medium
A. Runtime error
B. "Hello from Parent"
C. Compilation error due to missing override
D. "Hello from Child"

Solution

  1. Step 1: Understand function overriding

    The Child contract overrides the greet() function from Parent using override keyword.
  2. Step 2: Determine which function runs on Child

    Calling greet() on Child runs the overridden function returning "Hello from Child".
  3. Final Answer:

    "Hello from Child" -> Option D
  4. Quick Check:

    Override function output = "Hello from Child" [OK]
Hint: Child's override function runs, not parent's [OK]
Common Mistakes:
  • Thinking parent function runs instead of child
  • Missing 'override' keyword causing compile error
  • Expecting runtime error without cause
4. Identify the error in the following Solidity code snippet:
contract Base {
    function foo() public virtual returns (string memory) {
        return "Base";
    }
}

contract Derived is Base {
    function foo() public returns (string memory) {
        return "Derived";
    }
}
medium
A. Missing 'override' keyword in Derived's foo function
B. Missing 'virtual' keyword in Base's foo function
C. Derived contract should not inherit Base
D. Function foo should be private

Solution

  1. Step 1: Check overriding rules in Solidity

    When overriding a function marked virtual in the parent, the child function must use override keyword.
  2. Step 2: Identify missing keyword

    The foo() function in Derived lacks the override keyword, causing a compilation error.
  3. Final Answer:

    Missing 'override' keyword in Derived's foo function -> Option A
  4. Quick Check:

    Override missing = compile error [OK]
Hint: Override functions must use 'override' keyword [OK]
Common Mistakes:
  • Forgetting 'override' keyword in child function
  • Thinking 'virtual' is needed in child function
  • Assuming inheritance itself causes error
5. Given the following contracts, what will be the output of calling getMessage() on contract GrandChild?
contract Parent {
    function getMessage() public virtual returns (string memory) {
        return "Parent";
    }
}

contract Child is Parent {
    function getMessage() public virtual override returns (string memory) {
        return "Child";
    }
}

contract GrandChild is Child {
    function getMessage() public override returns (string memory) {
        return super.getMessage();
    }
}
hard
A. "Parent"
B. "Child"
C. "GrandChild"
D. Compilation error due to missing virtual

Solution

  1. Step 1: Understand super keyword in Solidity inheritance

    The GrandChild contract calls super.getMessage(), which refers to the immediate parent Child implementation.
  2. Step 2: Identify which function runs

    The Child contract's getMessage() returns "Child", so GrandChild returns "Child" as well.
  3. Final Answer:

    "Child" -> Option B
  4. Quick Check:

    super calls immediate parent = "Child" [OK]
Hint: super calls immediate parent function [OK]
Common Mistakes:
  • Thinking super calls grandparent function
  • Expecting 'GrandChild' string without override
  • Missing 'virtual' in intermediate contract causing error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard