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

Why interfaces are needed in C Sharp (C#) - Test Your Understanding

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] { string Speak(); }
Drag options to blanks, or click blank then click option'
AAnimalInterface
BAnimal
CIAnimal
DInterfaceAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using class names instead of interface names
Not starting 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 string Speak() { return "Woof!"; } }
Drag options to blanks, or click blank then click option'
ASpeakable
BDogInterface
CAnimal
DIAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of interface
Forgetting to implement the interface
3fill in blank
hard

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

C Sharp (C#)
public interface IAnimal { string [1](); }
Drag options to blanks, or click blank then click option'
ASpeak
BRun
CSleep
DEat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than in the class
Omitting the method name
4fill in blank
hard

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

C Sharp (C#)
var sounds = new Dictionary<string, string> { {"Dog", dog.[1]()}, {"Cat", cat.[2]()} };
Drag options to blanks, or click blank then click option'
ASpeak
BMeow
DBark
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names for dog and cat
Using animal sounds as method names
5fill in blank
hard

Fill all three blanks to define an interface, implement it in a class, and call its method.

C Sharp (C#)
public interface [1] { string [2](); } public class Bird : [3] { public string Speak() { return "Chirp!"; } }
Drag options to blanks, or click blank then click option'
AIAnimal
BSpeak
DIBird
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching interface and class names
Using method names not declared in the interface