Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Virtual and override keywords in Blockchain / Solidity - Practice Problems & Coding Challenges

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

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

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

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

    function test() public pure returns (string memory) {
        return greet();
    }
}
A"Hello from C"
B"Hello from B"
C"Hello from A"
DCompilation error due to missing override
Attempts:
2 left
💡 Hint
Remember that the most derived override function is called.
❓ Predict Output
intermediate
2:00remaining
Effect of missing override keyword in Solidity
What happens if contract B tries to override function greet() from contract A but does NOT use the override keyword?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

contract B is A {
    function greet() public pure returns (string memory) {
        return "Hello from B";
    }
}
ARuns and returns "Hello from B"
BCompilation error due to missing override keyword
CRuns and returns "Hello from A"
DRuntime error due to function conflict
Attempts:
2 left
💡 Hint
Solidity requires explicit override keyword when overriding virtual functions.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple inheritance with virtual and override
Given the following Solidity code, what is the cause of the compilation error?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

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

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

contract D is B, C {
    function foo() public override pure returns (string memory) {
        return super.foo();
    }
}
ANo error, compiles successfully
BMissing override specifier in contract D's foo()
CFunction foo() must be marked virtual in contract D
DAmbiguous call to super.foo() in contract D
Attempts:
2 left
💡 Hint
Consider how Solidity resolves multiple inheritance and super calls.
📝 Syntax
advanced
2:00remaining
Correct syntax for overriding a virtual function in Solidity
Which of the following function declarations correctly overrides a virtual function bar() from a base contract?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Base {
    function bar() public virtual pure returns (string memory) {
        return "Base";
    }
}

contract Derived is Base {
    // Choose the correct override syntax for bar()
}
Afunction bar() public override virtual pure returns (string memory) { return "Derived"; }
Bfunction bar() public virtual returns (string memory) { return "Derived"; }
Cfunction bar() public override pure returns (string memory) { return "Derived"; }
Dfunction bar() public pure returns (string memory) { return "Derived"; }
Attempts:
2 left
💡 Hint
Override keyword must be present and virtual is optional if no further override is needed.
🚀 Application
expert
3:00remaining
Determine the output of complex virtual and override usage
Consider the following Solidity contracts. What is the output of calling callFoo() on contract Final?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

contract Y is X {
    function foo() public virtual override pure returns (string memory) {
        return string(abi.encodePacked("Y", super.foo()));
    }
}

contract Z is X {
    function foo() public virtual override pure returns (string memory) {
        return string(abi.encodePacked("Z", super.foo()));
    }
}

contract Final is Y, Z {
    function foo() public override(Y, Z) pure returns (string memory) {
        return string(abi.encodePacked("Final", super.foo()));
    }

    function callFoo() public pure returns (string memory) {
        return foo();
    }
}
A"FinalZYX"
B"FinalXYZ"
C"FinalYZX"
D"FinalZX"
Attempts:
2 left
💡 Hint
Check the linearization order of inheritance and how super calls chain.

Practice

(1/5)
1. What does the virtual keyword do in a blockchain smart contract?
easy
A. Makes a function private
B. Prevents a function from being changed
C. Deletes a function from the contract
D. Allows a function to be changed in child contracts

Solution

  1. Step 1: Understand the role of virtual

    The virtual keyword marks a function so child contracts can change it.
  2. Step 2: Compare with other options

    Options B, C, and D describe behaviors that virtual does not do.
  3. Final Answer:

    Allows a function to be changed in child contracts -> Option D
  4. Quick Check:

    virtual = allows override [OK]
Hint: Virtual means function can be changed later [OK]
Common Mistakes:
  • Thinking virtual makes function private
  • Confusing virtual with override
  • Believing virtual deletes functions
2. Which of the following is the correct way to override a virtual function named transfer in a child contract?
easy
A. function transfer() public virtual { }
B. function transfer() public override { }
C. function transfer() public final { }
D. function transfer() public abstract { }

Solution

  1. Step 1: Identify override syntax

    To replace a virtual function, use override keyword in the child function.
  2. Step 2: Check options

    function transfer() public override { } uses override, correct for overriding. function transfer() public virtual { } uses virtual which allows override but does not override itself. Options C and D are incorrect keywords here.
  3. Final Answer:

    function transfer() public override { } -> Option B
  4. Quick Check:

    Override keyword replaces virtual function [OK]
Hint: Use override keyword to replace virtual function [OK]
Common Mistakes:
  • Using virtual instead of override to replace function
  • Using final or abstract incorrectly
  • Omitting override keyword
3. Consider this Solidity code snippet:
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";
    }
}

contract Test {
    function testGreet() public returns (string memory) {
        Child c = new Child();
        return c.greet();
    }
}

What will testGreet() return when called?
medium
A. "Hello from Parent"
B. Compilation error
C. "Hello from Child"
D. Runtime error

Solution

  1. Step 1: Understand virtual and override behavior

    The greet function in Parent is marked virtual, allowing Child to override it.
  2. Step 2: Check which function is called

    When c.greet() is called on a Child instance, the overridden greet in Child runs, returning "Hello from Child".
  3. Final Answer:

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

    Override function runs on child instance [OK]
Hint: Child override runs, not parent virtual function [OK]
Common Mistakes:
  • Expecting parent function output
  • Thinking override causes error
  • Confusing virtual with abstract
4. What is wrong with this Solidity code?
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. Base's foo function should not be virtual
C. Derived's foo function should be private
D. No errors, code is correct

Solution

  1. Step 1: Check override rules

    Since foo in Base is virtual, the overriding function in Derived must use override keyword.
  2. Step 2: Identify missing keyword

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

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

    Override keyword required when replacing virtual function [OK]
Hint: Override keyword is mandatory when replacing virtual functions [OK]
Common Mistakes:
  • Forgetting override keyword
  • Thinking virtual is optional in base
  • Making function private incorrectly
5. You have a contract hierarchy:
contract A {
    function calc() public virtual returns (uint) {
        return 1;
    }
}

contract B is A {
    function calc() public virtual override returns (uint) {
        return 2;
    }
}

contract C is B {
    function calc() public override returns (uint) {
        return 3;
    }
}

If you call calc() on a C contract instance, what is the output and why?
hard
A. 3, because C overrides B's function
B. 1, because A's function is virtual
C. 2, because B overrides A and is virtual
D. Compilation error due to multiple overrides

Solution

  1. Step 1: Trace overrides in inheritance

    Contract A defines calc as virtual. B overrides it and keeps it virtual. C overrides B's version without virtual.
  2. Step 2: Determine which function runs on C instance

    Calling calc() on C runs C's override, returning 3.
  3. Final Answer:

    3, because C overrides B's function -> Option A
  4. Quick Check:

    Most derived override runs on instance [OK]
Hint: Most derived override runs, even if base is virtual [OK]
Common Mistakes:
  • Thinking base virtual function runs
  • Confusing virtual with override
  • Expecting compilation error from multiple overrides
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