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

Interface as contract 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 an interface named IAnimal.

C Sharp (C#)
public interface [1] {
    void Speak();
}
Drag options to blanks, or click blank then click option'
AIAnimal
BAnimal
CInterfaceAnimal
DAnimalInterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of an interface name.
Not starting the interface name with 'I'.
2fill in blank
medium

Complete the code to make the Dog class implement the IAnimal interface.

C Sharp (C#)
public class Dog : [1] {
    public void Speak() {
        Console.WriteLine("Woof!");
    }
}
Drag options to blanks, or click blank then click option'
AIDog
BDogInterface
CIAnimal
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of the interface name after the colon.
Forgetting to implement the interface methods.
3fill in blank
hard

Fix the error in the code by completing the interface method declaration.

C Sharp (C#)
public interface IAnimal {
    [1] Speak();
}
Drag options to blanks, or click blank then click option'
Apublic
Bint
Cstring
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 'public' keyword inside interface method declarations.
Using a return type that does not match the implementation.
4fill in blank
hard

Fill both blanks to create a dictionary that maps animal names to their sounds using an interface and class.

C Sharp (C#)
Dictionary<string, string> animalSounds = new Dictionary<string, string> {
    {"Dog", [1],
    {"Cat", [2]
};
Drag options to blanks, or click blank then click option'
A"Woof"
B"Meow"
C"Bark"
D"Purr"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect animal sounds.
Forgetting to put quotes around string values.
5fill in blank
hard

Fill all three blanks to implement the IAnimal interface in the Cat class with a Speak method.

C Sharp (C#)
public class [1] : [2] {
    public [3] Speak() {
        Console.WriteLine("Meow");
    }
}
Drag options to blanks, or click blank then click option'
ACat
BIAnimal
Cvoid
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class or interface names.
Incorrect return type in the method declaration.