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

Is-a relationship mental model 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 that Dog is a type of Animal using inheritance.

C Sharp (C#)
class Animal {}
class Dog : [1] {}
Drag options to blanks, or click blank then click option'
AAnimal
BDog
CObject
DCreature
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class name after the colon.
Forgetting the colon and just writing the class name.
2fill in blank
medium

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

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

Fix the error in the code by completing the inheritance declaration.

C Sharp (C#)
class Vehicle {}
class Car [1] Vehicle {
    public void Drive() {
        Console.WriteLine("Driving");
    }
}
Drag options to blanks, or click blank then click option'
Ainherits
B:
Cextends
Dimplements
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java or other language keywords like 'extends' or 'inherits'.
Using 'implements' which is for interfaces.
4fill in blank
hard

Complete the code to create a class Cat that inherits from Animal and overrides the Speak method.

C Sharp (C#)
class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal sound");
    }
}
class Cat : Animal {
    public [1] void Speak() {
        Console.WriteLine("Meow");
    }
}
Drag options to blanks, or click blank then click option'
A:
Boverride
Cvirtual
Dimplements
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' instead of 'override' in the derived class.
Using 'implements' instead of ':' for inheritance.
5fill in blank
hard

Fill both blanks to define a class Bird that inherits from Animal, overrides Speak, and calls the base Speak method inside it.

C Sharp (C#)
class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal sound");
    }
}
class Bird : Animal {
    public [1] void Speak() {
        [2].Speak();
        Console.WriteLine("Chirp");
    }
}
Drag options to blanks, or click blank then click option'
A:
Boverride
Cbase
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call the base method inside the override.
Using 'virtual' instead of 'override' in the derived class.
Using wrong syntax for inheritance.