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

Sealed 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 a sealed class named 'Vehicle'.

C Sharp (C#)
public [1] class Vehicle
{
    public void Start() { }
}
Drag options to blanks, or click blank then click option'
Asealed
Babstract
Cstatic
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abstract' instead of 'sealed' which allows inheritance.
Using 'static' which makes the class static, not sealed.
2fill in blank
medium

Complete the code to prevent overriding of the 'Drive' method in derived classes.

C Sharp (C#)
public class Car
{
    public virtual void Drive() { }
}

public class SportsCar : Car
{
    public [1] override void Drive() { }
}
Drag options to blanks, or click blank then click option'
Aabstract
Bsealed
Cstatic
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abstract' which requires the method to be overridden.
Using 'virtual' which allows overriding.
3fill in blank
hard

Fix the error in the code by completing the class declaration to prevent inheritance.

C Sharp (C#)
public class BaseClass { }

public [1] class DerivedClass : BaseClass { }
Drag options to blanks, or click blank then click option'
Astatic
Babstract
Csealed
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abstract' which allows inheritance.
Using 'virtual' which is invalid on classes.
4fill in blank
hard

Fill both blanks to declare a sealed class and a sealed override method.

C Sharp (C#)
public class Animal
{
    public virtual void Speak() { }
}

public [1] class Dog : Animal
{
    public [2] override void Speak() { }
}
Drag options to blanks, or click blank then click option'
Asealed
Babstract
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abstract' instead of 'sealed' for the class.
Using 'virtual' instead of 'sealed' for the method.
5fill in blank
hard

Fill all three blanks to create a sealed class, a sealed override method, and call the base method.

C Sharp (C#)
public class Shape
{
    public virtual void Draw() { Console.WriteLine("Drawing shape"); }
}

public [1] class Circle : Shape
{
    public [2] override void Draw()
    {
        base.[3]();
        Console.WriteLine("Drawing circle");
    }
}
Drag options to blanks, or click blank then click option'
Asealed
BDraw
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Not sealing the class or method properly.
Calling a wrong base method name.