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

Abstract classes and methods 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 abstract class named Animal.

C Sharp (C#)
public [1] class Animal {
    public abstract void Speak();
}
Drag options to blanks, or click blank then click option'
Asealed
Babstract
Cstatic
Dpartial
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sealed' instead of 'abstract' which prevents inheritance.
Using 'static' which makes the class non-instantiable and non-inheritable.
2fill in blank
medium

Complete the code to declare an abstract method Speak inside the abstract class Animal.

C Sharp (C#)
public abstract class Animal {
    public [1] void Speak();
}
Drag options to blanks, or click blank then click option'
Aoverride
Bvirtual
Cabstract
Dsealed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' which requires a method body.
Using 'override' which is for overriding base class methods.
3fill in blank
hard

Fix the error in the derived class Dog that does not implement the abstract method Speak.

C Sharp (C#)
public class Dog : Animal {
    public [1] void Speak() {
        Console.WriteLine("Woof!");
    }
}
Drag options to blanks, or click blank then click option'
Aoverride
Babstract
Csealed
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abstract' in the derived class method which is not allowed.
Using 'virtual' which does not fulfill the abstract method requirement.
4fill in blank
hard

Fill both blanks to declare an abstract class Vehicle and an abstract method Move.

C Sharp (C#)
public [1] class Vehicle {
    public [2] void Move();
}
Drag options to blanks, or click blank then click option'
Aabstract
Bvirtual
Dsealed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'virtual' for the method without a body causes a syntax error.
Using 'sealed' for the class prevents inheritance.
5fill in blank
hard

Fill all three blanks to implement an abstract class Shape with an abstract method Area and a derived class Circle overriding Area.

C Sharp (C#)
public [1] class Shape {
    public [2] double Area();
}

public class Circle : Shape {
    private double radius;
    public Circle(double r) { radius = r; }
    public [3] double Area() {
        return Math.PI * radius * radius;
    }
}
Drag options to blanks, or click blank then click option'
Aabstract
Coverride
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'override' in the derived class method causes a compile error.
Using 'virtual' instead of 'abstract' for the base method without a body.