Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the sealed keyword do when applied to a class in C#?
easy
A. Prevents the class from being inherited by other classes.
B. Allows the class to be inherited multiple times.
C. Makes the class abstract and uninstantiable.
D. Enables the class to override methods from its base class.

Solution

  1. Step 1: Understand the sealed keyword on classes

    The sealed keyword on a class means no other class can inherit from it.
  2. Step 2: Compare options with this meaning

    Only Prevents the class from being inherited by other classes. correctly states that the class cannot be inherited.
  3. Final Answer:

    Prevents the class from being inherited by other classes. -> Option A
  4. Quick Check:

    Sealed class = no inheritance [OK]
Hint: Sealed class means no child classes allowed [OK]
Common Mistakes:
  • Thinking sealed classes can be inherited
  • Confusing sealed with abstract
  • Assuming sealed allows overriding
2. Which of the following is the correct syntax to declare a sealed method in C#?
easy
A. sealed public void MyMethod() { }
B. public override sealed void MyMethod() { }
C. public sealed void MyMethod() { }
D. override sealed public void MyMethod() { }

Solution

  1. Step 1: Recall sealed method syntax

    A method can only be sealed if it overrides a base method, so it must have override sealed modifiers.
  2. Step 2: Check options for correct order and modifiers

    public override sealed void MyMethod() { } correctly uses public override sealed void MyMethod(). Other options miss override or have wrong order.
  3. Final Answer:

    public override sealed void MyMethod() { } -> Option B
  4. Quick Check:

    Sealed method = override + sealed [OK]
Hint: Sealed methods must override and use 'override sealed' [OK]
Common Mistakes:
  • Declaring sealed method without override
  • Wrong order of modifiers
  • Missing override keyword
3. What will be the output of the following code?
class Base {
    public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
    public sealed override void Show() { Console.WriteLine("Derived"); }
}
class MoreDerived : Derived {
    public override void Show() { Console.WriteLine("MoreDerived"); }
}

var obj = new MoreDerived();
obj.Show();
medium
A. Base
B. Derived
C. MoreDerived
D. Compilation error

Solution

  1. Step 1: Understand sealed override effect

    The method Show in Derived is sealed, so it cannot be overridden in MoreDerived.
  2. Step 2: Check the code for override in MoreDerived

    MoreDerived tries to override Show, which causes a compilation error.
  3. Final Answer:

    Compilation error -> Option D
  4. Quick Check:

    Sealed method cannot be overridden [OK]
Hint: Sealed override blocks further overrides causing errors [OK]
Common Mistakes:
  • Assuming MoreDerived.Show runs
  • Ignoring sealed keyword effect
  • Thinking output is Derived
4. Identify the error in this code snippet:
sealed class Animal {
    public void Speak() { Console.WriteLine("Animal speaks"); }
}
class Dog : Animal {
    public void Speak() { Console.WriteLine("Dog barks"); }
}
medium
A. Method Speak must be virtual in Animal.
B. Cannot declare method Speak in Dog class.
C. Cannot inherit from sealed class Animal.
D. No error, code runs fine.

Solution

  1. Step 1: Check sealed class inheritance rules

    A sealed class cannot be inherited by any other class.
  2. Step 2: Analyze Dog class inheritance

    Dog tries to inherit from sealed Animal, which causes a compilation error.
  3. Final Answer:

    Cannot inherit from sealed class Animal. -> Option C
  4. Quick Check:

    Sealed class blocks inheritance [OK]
Hint: Sealed class cannot have child classes [OK]
Common Mistakes:
  • Thinking method override causes error
  • Ignoring sealed class inheritance rule
  • Assuming code compiles fine
5. You have a base class Vehicle with a virtual method Start(). You want to create a class Car that overrides Start() but prevents any further subclass from overriding it. How should you declare Start() in Car?
hard
A. public override sealed void Start() { }
B. public sealed void Start() { }
C. public override void Start() sealed { }
D. sealed public override void Start() { }

Solution

  1. Step 1: Understand method sealing rules

    To prevent further overrides, the method must be both override and sealed.
  2. Step 2: Check correct syntax for sealed override

    The correct syntax is public override sealed void Start(). Other options have wrong order or missing keywords.
  3. Final Answer:

    public override sealed void Start() { } -> Option A
  4. Quick Check:

    Sealed override method syntax = override sealed [OK]
Hint: Use 'override sealed' to block further overrides [OK]
Common Mistakes:
  • Omitting override keyword
  • Wrong order of sealed and override
  • Trying to seal without overriding