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
Recall & Review
beginner
What is a sealed class in C#?
A sealed class is a class that cannot be inherited by any other class. It is used to prevent further extension of the class.
Click to reveal answer
beginner
What happens if you try to inherit from a sealed class?
The compiler will give an error because sealed classes cannot be used as base classes.
Click to reveal answer
intermediate
What is a sealed method in C#?
A sealed method is an override method that cannot be further overridden in derived classes. It stops the method from being changed again.
Click to reveal answer
intermediate
How do you declare a sealed method in C#?
You declare a sealed method by using the 'sealed' keyword along with 'override' in the method signature, like: public sealed override void MethodName() { }
Click to reveal answer
intermediate
Why would you use sealed classes or methods?
To improve security and performance by preventing unwanted inheritance or method overriding, and to clearly define the class or method behavior.
Click to reveal answer
What keyword is used to prevent a class from being inherited in C#?
Astatic
Babstract
Csealed
Dvirtual
✗ Incorrect
The 'sealed' keyword prevents a class from being inherited.
Which of the following is true about a sealed method?
AIt must be static.
BIt can be overridden in derived classes.
CIt cannot be called from derived classes.
DIt cannot be overridden further once sealed.
✗ Incorrect
A sealed method cannot be overridden further in derived classes.
Can a sealed class have methods that are overridden?
AYes, if the methods are virtual.
BNo, because the class is sealed.
CYes, but only if the methods are sealed too.
DNo, methods cannot be overridden in any class.
✗ Incorrect
A sealed class can have virtual methods, but since the class cannot be inherited, overriding is not possible outside.
How do you declare a method that cannot be overridden further in a derived class?
AUse 'static' keyword.
BUse 'sealed' keyword with 'override'.
CUse 'abstract' keyword.
DUse 'virtual' keyword.
✗ Incorrect
You use 'sealed override' to stop further overriding of a method.
What error occurs if you try to inherit from a sealed class?
ACompiler error
BLogical error
CRuntime exception
DNo error
✗ Incorrect
The compiler will show an error because sealed classes cannot be inherited.
Explain what a sealed class is and why you might use it.
Think about stopping others from extending your class.
You got /3 concepts.
Describe how to declare a sealed method and what effect it has on inheritance.
Focus on method overriding and stopping it.
You got /3 concepts.
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
Step 1: Understand the sealed keyword on classes
The sealed keyword on a class means no other class can inherit from it.
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.
Final Answer:
Prevents the class from being inherited by other classes. -> Option A
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
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.
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.
Final Answer:
public override sealed void MyMethod() { } -> Option B
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
Step 1: Understand sealed override effect
The method Show in Derived is sealed, so it cannot be overridden in MoreDerived.
Step 2: Check the code for override in MoreDerived
MoreDerived tries to override Show, which causes a compilation error.
Final Answer:
Compilation error -> Option D
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
Step 1: Check sealed class inheritance rules
A sealed class cannot be inherited by any other class.
Step 2: Analyze Dog class inheritance
Dog tries to inherit from sealed Animal, which causes a compilation error.
Final Answer:
Cannot inherit from sealed class Animal. -> Option C
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
Step 1: Understand method sealing rules
To prevent further overrides, the method must be both override and sealed.
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.
Final Answer:
public override sealed void Start() { } -> Option A