0
0
Blockchain / Solidityprogramming~10 mins

Virtual and override keywords in Blockchain / Solidity - Interactive Code Practice

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

Complete the code to declare a virtual function in a smart contract.

Blockchain / Solidity
contract Base {
    function greet() public view [1] returns (string memory) {
        return "Hello from Base";
    }
}
Drag options to blanks, or click blank then click option'
Aexternal
Bvirtual
Coverride
Dpure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' instead of 'virtual' when declaring the base function.
Omitting the keyword entirely, which prevents overriding.
2fill in blank
medium

Complete the code to override the virtual function in the derived contract.

Blockchain / Solidity
contract Derived is Base {
    function greet() public view [1] returns (string memory) {
        return "Hello from Derived";
    }
}
Drag options to blanks, or click blank then click option'
Aoverride
Bpure
Cexternal
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' instead of 'override' when overriding a function.
Forgetting to mark the base function as virtual.
3fill in blank
hard

Fix the error in the derived contract by adding the correct keyword to the overridden function.

Blockchain / Solidity
contract Derived is Base {
    function greet() public view [1] returns (string memory) {
        return "Hello from Derived";
    }
}
Drag options to blanks, or click blank then click option'
Aoverride
Bvirtual
Cexternal
Dpayable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' instead of 'override' in the derived contract.
Omitting the keyword entirely, causing a compilation error.
4fill in blank
hard

Fill both blanks to declare a virtual function and override it correctly.

Blockchain / Solidity
contract Base {
    function calculate() public view [1] returns (uint) {
        return 1;
    }
}

contract Derived is Base {
    function calculate() public view [2] returns (uint) {
        return 2;
    }
}
Drag options to blanks, or click blank then click option'
Avirtual
Bexternal
Coverride
Dpure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' in the base function instead of 'virtual'.
Forgetting to use 'override' in the derived function.
5fill in blank
hard

Fill all three blanks to declare a virtual function, override it, and call the base function inside the override.

Blockchain / Solidity
contract Base {
    function info() public view [3] returns (string memory) {
        return "Base info";
    }
}

contract Derived is Base {
    function info() public view [1] returns (string memory) {
        return string(abi.encodePacked(Base.[2](), " and Derived info"));
    }
}
Drag options to blanks, or click blank then click option'
Aoverride
Binfo
Cvirtual
Dpure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' instead of 'override' in the derived function.
Calling a wrong function name instead of the base function.
Not marking the base function as virtual.