0
0
C Sharp (C#)programming~10 mins

Base keyword behavior in C Sharp (C#) - Interactive Code Practice

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

Complete 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'
Abase
Bthis
Csuper
Dparent
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.
2fill in blank
medium

Complete 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'
Athis
Bbase
Csuper
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base constructor.
Using 'super' which is not valid in C#.
3fill in blank
hard

Fix 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'
Abase
Bthis
Csuper
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' which calls the current class method causing recursion.
Using 'super' which is not a C# keyword.
4fill in blank
hard

Fill 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'
Abase
Bthis
Csuper
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'super' or 'parent' which are invalid in C#.
Using 'this' to call the base method causing recursion.
5fill in blank
hard

Fill 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'
Athis
BcircleArea
Cbase
Dradius
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base method.
Returning only circleArea without adding baseArea.