Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Why inheritance promotes code reuse in Blockchain / Solidity - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Inheritance Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Solidity inheritance example
What is the output of this Solidity contract when calling getMessage() from contract Child?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

contract Child is Parent {
    function getMessage() public pure override returns (string memory) {
        return "Hello from Child";
    }
}
A"Hello from Child"
BRuntime error due to inheritance conflict
CCompilation error due to missing override
D"Hello from Parent"
Attempts:
2 left
💡 Hint
Look at which function is called in the Child contract and how override works.
🧠 Conceptual
intermediate
1:30remaining
Why inheritance helps code reuse in smart contracts
Which of the following best explains why inheritance promotes code reuse in blockchain smart contracts?
AIt prevents contracts from calling functions of other contracts.
BIt forces contracts to duplicate code for security reasons.
CIt allows contracts to share and extend existing code without rewriting it.
DIt makes contracts run faster by removing all function calls.
Attempts:
2 left
💡 Hint
Think about how inheritance lets you build on top of existing code.
🔧 Debug
advanced
2:00remaining
Identify the error in Solidity inheritance
What error will this Solidity code produce?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Base {
    function greet() public pure returns (string memory) {
        return "Hi";
    }
}

contract Derived is Base {
    function greet() public pure returns (string memory) {
        return "Hello";
    }
}
ACompilation error: Function must be marked override in Derived
BNo error, code compiles and greet returns "Hello"
CRuntime error: greet function not found
DCompilation error: Missing virtual keyword in Base
Attempts:
2 left
💡 Hint
Check if the override keyword is used correctly when redefining functions.
📝 Syntax
advanced
1:30remaining
Correct syntax for multiple inheritance in Solidity
Which option shows the correct syntax for a contract inheriting from two contracts A and B in Solidity?
Acontract C is A, B {}
Bcontract C inherits A and B {}
Ccontract C : A, B {}
Dcontract C extends A, B {}
Attempts:
2 left
💡 Hint
Look at how Solidity uses the 'is' keyword for inheritance.
🚀 Application
expert
2:30remaining
How inheritance reduces gas costs in smart contracts
How does using inheritance in smart contracts help reduce gas costs during deployment and execution?
AInheritance disables expensive function calls to save gas.
BInheritance automatically compresses bytecode to half its size.
CIt allows contracts to run off-chain, saving gas.
DBy reusing code, contracts avoid duplicating logic, reducing contract size and gas fees.
Attempts:
2 left
💡 Hint
Think about how code reuse affects contract size and deployment cost.

Practice

(1/5)
1. Why does inheritance promote code reuse in blockchain smart contracts?
easy
A. It allows new contracts to use existing code without rewriting it
B. It makes contracts run faster on the blockchain
C. It encrypts the contract code automatically
D. It prevents contracts from being copied

Solution

  1. Step 1: Understand inheritance concept

    Inheritance lets a new contract use code from an existing contract, avoiding rewriting.
  2. Step 2: Identify how this saves effort

    By reusing code, developers save time and reduce errors, making maintenance easier.
  3. Final Answer:

    It allows new contracts to use existing code without rewriting it -> Option A
  4. Quick Check:

    Inheritance = reuse code [OK]
Hint: Inheritance means reuse existing code easily [OK]
Common Mistakes:
  • Thinking inheritance speeds up contract execution
  • Confusing inheritance with encryption
  • Believing inheritance blocks code copying
2. Which of the following is the correct Solidity syntax to inherit from a contract named BaseContract?
easy
A. contract NewContract inherits BaseContract {}
B. contract NewContract from BaseContract {}
C. contract NewContract extends BaseContract {}
D. contract NewContract is BaseContract {}

Solution

  1. Step 1: Recall Solidity inheritance syntax

    Solidity uses is keyword to inherit from another contract.
  2. Step 2: Match syntax with options

    contract NewContract is BaseContract {} uses is correctly; others use invalid keywords.
  3. Final Answer:

    contract NewContract is BaseContract {} -> Option D
  4. Quick Check:

    Solidity inheritance uses 'is' [OK]
Hint: Remember Solidity uses 'is' for inheritance [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' instead of 'is'
  • Forgetting curly braces after contract declaration
  • Mixing syntax from other languages
3. Given the following Solidity code, what will be the output of getValue() when called on Child contract?
contract Parent {
    function getValue() public pure returns (string memory) {
        return "Parent";
    }
}

contract Child is Parent {
    function getValue() public pure returns (string memory) {
        return "Child";
    }
}
medium
A. "Parent"
B. "Child"
C. Compilation error due to duplicate function
D. Runtime error

Solution

  1. Step 1: Understand function overriding in inheritance

    The Child contract overrides getValue() from Parent.
  2. Step 2: Determine which function runs on Child

    Calling getValue() on Child runs the overridden version returning "Child".
  3. Final Answer:

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

    Override method runs in child contract [OK]
Hint: Child overrides parent method, so child's version runs [OK]
Common Mistakes:
  • Assuming parent method runs instead of child's
  • Thinking duplicate functions cause compile error
  • Confusing runtime errors with override behavior
4. Identify the error in this Solidity inheritance code:
contract A {
    uint public x = 5;
}

contract B is A {
    uint public x = 10;
}
medium
A. Variable x is redeclared in child contract causing shadowing
B. Missing constructor in child contract
C. Incorrect inheritance keyword used
D. No error, code is correct

Solution

  1. Step 1: Check variable declarations in parent and child

    Both A and B declare x, causing shadowing.
  2. Step 2: Understand shadowing issue

    Child's x hides parent's x, which can cause confusion or bugs.
  3. Final Answer:

    Variable x is redeclared in child contract causing shadowing -> Option A
  4. Quick Check:

    Variable redeclaration causes shadowing [OK]
Hint: Avoid redeclaring variables in child contracts [OK]
Common Mistakes:
  • Ignoring variable shadowing effects
  • Thinking constructors are mandatory for inheritance
  • Confusing inheritance keyword syntax
5. You want to create a smart contract that reuses code from two existing contracts, Token and Ownable. Which approach best promotes code reuse and avoids duplication?
hard
A. Write new code that mimics Token and Ownable functions
B. Copy all code from Token and Ownable into the new contract
C. Create a new contract that inherits from both Token and Ownable
D. Deploy Token and Ownable separately and call them externally

Solution

  1. Step 1: Understand multiple inheritance in Solidity

    Solidity supports inheriting from multiple contracts to reuse their code.
  2. Step 2: Compare options for code reuse

    Inheriting from both Token and Ownable avoids duplication and keeps code organized.
  3. Final Answer:

    Create a new contract that inherits from both Token and Ownable -> Option C
  4. Quick Check:

    Multiple inheritance promotes reuse best [OK]
Hint: Use multiple inheritance to combine existing contracts [OK]
Common Mistakes:
  • Copy-pasting code instead of inheriting
  • Rewriting code manually causing errors
  • Ignoring Solidity's multiple inheritance feature
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