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

Virtual method dispatch mechanism 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 virtual method named Speak.

C Sharp (C#)
public class Animal {
    public [1] void Speak() {
        Console.WriteLine("Animal speaks");
    }
}
Drag options to blanks, or click blank then click option'
Astatic
Boverride
Csealed
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' instead of 'virtual' in base class method declaration.
2fill in blank
medium

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

C Sharp (C#)
public class Dog : Animal {
    public [1] void Speak() {
        Console.WriteLine("Dog barks");
    }
}
Drag options to blanks, or click blank then click option'
Avirtual
Bnew
Coverride
Dsealed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' instead of 'override' in derived class method.
3fill in blank
hard

Fix the error in the method declaration to correctly override the base class method.

C Sharp (C#)
public class Cat : Animal {
    public [1] void Speak() {
        Console.WriteLine("Cat meows");
    }
}
Drag options to blanks, or click blank then click option'
Anew
Boverride
Cvirtual
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' or 'new' instead of 'override' in derived class.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps animal names to their Speak method outputs if the name length is greater than 3.

C Sharp (C#)
var sounds = new Dictionary<string, string> {
    {"Dog", "Bark"},
    {"Cat", "Meow"},
    {"Horse", "Neigh"}
};

var filtered = sounds.Where(kv => kv.Key.[1] > 3)
                      .ToDictionary(kv => kv.Key, kv => kv.[2]);
Drag options to blanks, or click blank then click option'
ALength
BValue
CKey
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' instead of 'Length' for string length.
Using 'Key' instead of 'Value' for dictionary value.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase animal names to their sounds if the sound length is greater than 3.

C Sharp (C#)
var sounds = new Dictionary<string, string> {
    {"Dog", "Bark"},
    {"Cat", "Meow"},
    {"Horse", "Neigh"}
};

var result = sounds.Where(kv => kv.Value.[1] > 3)
                   .ToDictionary(kv => kv.Key.[2](), kv => kv.[3]);
Drag options to blanks, or click blank then click option'
ALength
BValue
CToUpper
DKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' instead of 'Length'.
Using 'Key' instead of 'Value' for the dictionary value.