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

Sealed classes and methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sealed Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of sealed class inheritance attempt
What will be the output or result of this C# code?
C Sharp (C#)
sealed class Animal {
    public void Speak() {
        System.Console.WriteLine("Animal speaks");
    }
}

class Dog : Animal {
}

class Program {
    static void Main() {
        Dog d = new Dog();
        d.Speak();
    }
}
ACompilation error: method 'Speak' is sealed
BAnimal speaks
CRuntime error: method not found
DCompilation error: cannot derive from sealed class 'Animal'
Attempts:
2 left
💡 Hint
Remember what sealed means for classes in C#.
Predict Output
intermediate
2:00remaining
Output of sealed method override attempt
What will be the output or result of this C# code?
C Sharp (C#)
class Vehicle {
    public virtual void Start() {
        System.Console.WriteLine("Vehicle started");
    }
}

class Car : Vehicle {
    public sealed override void Start() {
        System.Console.WriteLine("Car started");
    }
}

class SportsCar : Car {
    public override void Start() {
        System.Console.WriteLine("SportsCar started");
    }
}

class Program {
    static void Main() {
        SportsCar sc = new SportsCar();
        sc.Start();
    }
}
AVehicle started
BCompilation error: cannot override sealed method 'Start'
CCar started
DSportsCar started
Attempts:
2 left
💡 Hint
Check what sealed means when applied to methods.
🧠 Conceptual
advanced
1:30remaining
Purpose of sealed keyword in C#
Which of the following best describes the purpose of the sealed keyword in C#?
ATo prevent a class from being inherited or a method from being overridden further
BTo make a class abstract and force implementation in derived classes
CTo allow multiple inheritance of classes
DTo mark a method as asynchronous
Attempts:
2 left
💡 Hint
Think about what sealed restricts in inheritance.
Predict Output
advanced
2:00remaining
Output of sealed method call via base reference
What will be the output of this C# program?
C Sharp (C#)
class Base {
    public virtual void Show() {
        System.Console.WriteLine("Base Show");
    }
}

class Derived : Base {
    public sealed override void Show() {
        System.Console.WriteLine("Derived Show");
    }
}

class MoreDerived : Derived {
    // No override here
}

class Program {
    static void Main() {
        Base obj = new MoreDerived();
        obj.Show();
    }
}
ABase Show
BCompilation error: cannot override sealed method
CDerived Show
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Consider which method is called when sealed override exists and no further override.
🔧 Debug
expert
2:30remaining
Identify the error in sealed method usage
What error will this C# code produce?
C Sharp (C#)
class Parent {
    public virtual void Display() {
        System.Console.WriteLine("Parent Display");
    }
}

class Child : Parent {
    public sealed override void Display() {
        System.Console.WriteLine("Child Display");
    }
}

class GrandChild : Child {
    public override void Display() {
        System.Console.WriteLine("GrandChild Display");
    }
}

class Program {
    static void Main() {
        GrandChild gc = new GrandChild();
        gc.Display();
    }
}
ACompilation error: cannot override sealed method 'Display' in 'Child'
BGrandChild Display
CRuntime error: method dispatch failed
DChild Display
Attempts:
2 left
💡 Hint
Check the rules about overriding sealed methods.