0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inherit properties from the parent contract.

Blockchain / Solidity
contract Child is [1] {
    // Child contract code
}
Drag options to blanks, or click blank then click option'
AParent
BChild
CBase
DContract
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child contract's own name instead of the parent.
Forgetting to specify the parent contract name.
2fill in blank
medium

Complete the code to call the parent constructor in the child contract.

Blockchain / Solidity
contract Child is Parent {
    constructor() [1] {
        Parent();
        // Initialization
    }
}
Drag options to blanks, or click blank then click option'
Ainternal
Bpublic
Cexternal
Doverride
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' keyword incorrectly.
Setting constructor visibility to external.
3fill in blank
hard

Fix the error in overriding the parent function in the child contract.

Blockchain / Solidity
contract Child is Parent {
    function greet() [1] override returns (string memory) {
        return "Hello from Child!";
    }
}
Drag options to blanks, or click blank then click option'
Avirtual
Boverride
Cpublic
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Placing 'override' as visibility.
Missing 'override' keyword after visibility.
4fill in blank
hard

Fill both blanks to define a function in the parent contract that can be overridden by the child.

Blockchain / Solidity
contract Parent {
    function greet() public [1] returns (string memory) {
        return "Hello from Parent!";
    }
}

contract Child is Parent {
    function greet() public [2] returns (string memory) {
        return "Hello from Child!";
    }
}
Drag options to blanks, or click blank then click option'
Avirtual
Boverride
Cexternal
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'virtual' in parent function.
Not using 'override' in child function.
5fill in blank
hard

Fill all three blanks to create a reusable modifier in the parent and apply it in the child contract.

Blockchain / Solidity
contract Parent {
    modifier onlyOwner() [1] {
        require(msg.sender == owner, "Not owner");
        _;
    }
}

contract Child is Parent {
    function secureAction() [2] [3] onlyOwner {
        // secure code
    }
}
Drag options to blanks, or click blank then click option'
Apublic
Bvirtual
Coverride
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Making modifier public.
Missing 'override' keyword in child function.
Wrong function visibility.