Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

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

Practice

(1/5)
1.

What is the main purpose of multiple inheritance in blockchain smart contracts?

easy
A. To restrict a contract to inherit from only one contract
B. To allow a contract to use code from more than one contract
C. To make contracts run faster on the blockchain
D. To prevent contracts from sharing any code

Solution

  1. Step 1: Understand inheritance in contracts

    Inheritance allows a contract to reuse code from another contract.
  2. Step 2: Identify multiple inheritance meaning

    Multiple inheritance means inheriting from more than one contract to combine features.
  3. Final Answer:

    To allow a contract to use code from more than one contract -> Option B
  4. Quick Check:

    Multiple inheritance = use code from multiple contracts [OK]
Hint: Multiple inheritance means using code from many contracts [OK]
Common Mistakes:
  • Thinking multiple inheritance restricts to one contract
  • Confusing inheritance with contract speed
  • Believing inheritance prevents code sharing
2.

Which of the following is the correct syntax to inherit from two contracts A and B in Solidity?

contract C is ? {}
easy
A. A + B
B. A & B
C. A | B
D. A, B

Solution

  1. Step 1: Recall Solidity inheritance syntax

    In Solidity, multiple inheritance uses commas to separate parent contracts.
  2. Step 2: Check the options

    Only A, B uses commas correctly: contract C is A, B {}.
  3. Final Answer:

    A, B -> Option D
  4. Quick Check:

    Use commas to list parents in Solidity inheritance [OK]
Hint: Use commas to list multiple parents in Solidity [OK]
Common Mistakes:
  • Using symbols like & or + instead of commas
  • Forgetting to separate parent contracts
  • Writing inheritance without any separator
3.

Consider the following Solidity contracts:

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

contract B {
    function greet() public pure virtual returns (string memory) {
        return "Hello from B";
    }
}

contract C is A, B {
    function greet() public pure override(A, B) returns (string memory) {
        return super.greet();
    }
}

What will C().greet() return?

medium
A. "Hello from A"
B. "Hello from C"
C. Compilation error due to ambiguity
D. "Hello from B"

Solution

  1. Step 1: Understand method resolution order

    In Solidity, the order of inheritance matters. The first parent contract's function is called by super.
  2. Step 2: Check inheritance order and super call

    Contract C inherits A then B. Calling super.greet() calls A's greet due to C3 linearization order C, A, B.
  3. Final Answer:

    "Hello from A" -> Option A
  4. Quick Check:

    First parent function called by super = "Hello from A" [OK]
Hint: In Solidity, super calls first inherited (leftmost) contract's method [OK]
Common Mistakes:
  • Assuming super calls last parent
  • Expecting compilation error without override
  • Thinking super calls child's own method
4.

Given the contracts below, what is the error in contract C?

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

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

contract C is A, B {
    // Missing override keyword
    function foo() public pure returns (string memory) {
        return super.foo();
    }
}
medium
A. Missing override keyword causes compilation error
B. super.foo() calls an undefined function
C. No error, code compiles and runs fine
D. Function foo() must be private

Solution

  1. Step 1: Check function overriding rules

    When overriding functions from multiple parents, Solidity requires the override keyword.
  2. Step 2: Identify missing override keyword

    Contract C's foo lacks override(A, B), causing a compilation error.
  3. Final Answer:

    Missing override keyword causes compilation error -> Option A
  4. Quick Check:

    Override keyword required when overriding multiple parents [OK]
Hint: Always add override keyword when overriding multiple parents [OK]
Common Mistakes:
  • Forgetting override keyword in child function
  • Assuming super.foo() is undefined
  • Thinking function visibility must be private
5.

You have two contracts:

contract Token {
    function transfer() public pure virtual returns (string memory) {
        return "Token transfer";
    }
}

contract Mintable {
    function transfer() public pure virtual returns (string memory) {
        return "Mintable transfer";
    }
}

contract MyToken is Token, Mintable {
    function transfer() public pure override(Token, Mintable) returns (string memory) {
        return string.concat(Token.transfer(), " & ", Mintable.transfer());
    }
}

What will MyToken().transfer() return?

hard
A. "Mintable transfer & Token transfer"
B. Compilation error due to ambiguous override
C. "Token transfer & Mintable transfer"
D. "Token transfer"

Solution

  1. Step 1: Understand explicit calls to parent functions

    MyToken's transfer calls both Token.transfer() and Mintable.transfer() explicitly.
  2. Step 2: Check string concatenation order

    The returned string concatenates Token.transfer() first, then Mintable.transfer() with " & " in between.
  3. Final Answer:

    "Token transfer & Mintable transfer" -> Option C
  4. Quick Check:

    Explicit calls combine both parents' transfer strings [OK]
Hint: Explicitly call each parent to combine their outputs [OK]
Common Mistakes:
  • Assuming super calls only one parent
  • Expecting compilation error without override
  • Mixing order of concatenation
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