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

Runtime polymorphism execution 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 declare a base class method that can be overridden.

C Sharp (C#)
public class Animal {
    public virtual void Speak() {
        Console.[1]("Animal speaks");
    }
}
Drag options to blanks, or click blank then click option'
ADisplay
BPrint
COutput
DWriteLine
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist in Console class like Print or Display.
2fill in blank
medium

Complete the code to override the Speak method in the derived class.

C Sharp (C#)
public class Dog : Animal {
    public override void [1]() {
        Console.WriteLine("Dog barks");
    }
}
Drag options to blanks, or click blank then click option'
ASpeak
BTalk
CSound
DMakeNoise
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name that does not override the base method.
3fill in blank
hard

Fix the error in calling the Speak method using a base class reference to a derived class object.

C Sharp (C#)
Animal myAnimal = new Dog();
myAnimal.[1]();
Drag options to blanks, or click blank then click option'
ABark
BTalk
CSpeak
DMakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that only exists in the derived class or using wrong method names.
4fill in blank
hard

Fill both blanks to create a base class reference to a derived class object and call the overridden method.

C Sharp (C#)
Animal [1] = new [2]();
[1].Speak();
Drag options to blanks, or click blank then click option'
AmyAnimal
BDog
CAnimal
DanimalObj
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base class name for the object creation or mismatching variable names.
5fill in blank
hard

Fill all three blanks to implement runtime polymorphism with two derived classes overriding the base method.

C Sharp (C#)
public class Cat : Animal {
    public override void [1]() {
        Console.WriteLine("Cat meows");
    }
}

Animal [2] = new Dog();
Animal [3] = new Cat();
[2].Speak();
[3].Speak();
Drag options to blanks, or click blank then click option'
ASpeak
BdogAnimal
CcatAnimal
DMakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names or inconsistent variable names.