Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the base class method from the derived class.
C Sharp (C#)
public class Animal { public virtual string Speak() { return "Animal sound"; } } public class Dog : Animal { public override string Speak() { return [1].Speak(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base class method.
Using 'super' which is not a C# keyword.
✗ Incorrect
The 'base' keyword in C# is used to access members of the base class from a derived class.
2fill in blank
mediumComplete the constructor to call the base class constructor with a parameter.
C Sharp (C#)
public class Vehicle { public string Brand; public Vehicle(string brand) { Brand = brand; } } public class Car : Vehicle { public Car(string brand) : [1](brand) { } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base constructor.
Using 'super' which is not valid in C#.
✗ Incorrect
In C#, the 'base' keyword is used to call the base class constructor from a derived class constructor.
3fill in blank
hardFix the error by correctly calling the base class method inside the overridden method.
C Sharp (C#)
public class Printer { public virtual void Print() { Console.WriteLine("Printing from Printer"); } } public class ColorPrinter : Printer { public override void Print() { [1].Print(); Console.WriteLine("Printing in color"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' which calls the current class method causing recursion.
Using 'super' which is not a C# keyword.
✗ Incorrect
The 'base' keyword is used to call the base class method from an overridden method in C#.
4fill in blank
hardFill both blanks to correctly override a method and call the base method inside it.
C Sharp (C#)
public class Logger { public virtual void Log(string message) { Console.WriteLine("Log: " + message); } } public class FileLogger : Logger { public override void Log(string message) { [1].Log(message); Console.WriteLine("Writing to file"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'super' or 'parent' which are invalid in C#.
Using 'this' to call the base method causing recursion.
✗ Incorrect
Use 'base' to call the base class method and 'this' to refer to the current instance if needed.
5fill in blank
hardFill all three blanks to override a method, call the base method, and add extra behavior.
C Sharp (C#)
public class Shape { public virtual double Area() { return 0; } } public class Circle : Shape { private double radius; public Circle(double radius) { this.radius = radius; } public override double Area() { double baseArea = [1].Area(); double circleArea = Math.PI * radius * radius; return baseArea + [2]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base method.
Returning only circleArea without adding baseArea.
✗ Incorrect
Use 'base' to call the base method, add 'circleArea' to it, and 'this' refers to the current instance.