What is the output of the following?
class A { public void Test() { Console.WriteLine("A"); } }
class B : A { public new void Test() { Console.WriteLine("B"); } }
A a = new B();
a.Test();
Answer: A
Using 'new' keyword hides the method. Since reference is of type A, A's Test() is called. Output is 'A'.
Q.2Hard
Consider the following code. What will be the output?
interface I1 { void Show(); }
interface I2 { void Show(); }
class C : I1, I2 { public void Show() { Console.WriteLine("C"); } }
C obj = new C();
obj.Show();
Answer: A
A single method implementation satisfies both interface contracts. The method is called and prints 'C'.
Q.3Hard
What will happen if you try to override a non-virtual method?
Answer: B
To override a method, it must be marked as 'virtual' in the base class. Attempting to override a non-virtual method causes a compilation error.
Q.4Hard
What is the output of the following code?
class Base { public virtual void Method() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Method() { base.Method(); Console.WriteLine("Derived"); } }
Derived d = new Derived();
d.Method();
Answer: C
The 'base' keyword calls the parent class method first, printing 'Base', then the derived method prints 'Derived'. Output: 'BaseDerived'.
Q.5Hard
In C#, what happens when you apply the 'static' modifier to a method inside an interface (C# 11+)?
Answer: B
C# 11 allows static abstract members in interfaces. Static methods in interfaces provide default implementations and can be called via the interface type.
Advertisement
Q.6Hard
In C# 10, what does the 'file' access modifier allow?
Answer: B
The 'file' scoped type modifier (C# 11) restricts a type to be visible only within the file it's declared in, preventing accidental public API expansion.
Q.7Hard
Which feature in C# 8+ allows you to provide default implementation in an interface?
Answer: B
C# 8 introduced default interface implementations, allowing interfaces to have method bodies that implementing classes can inherit or override.
Q.8Hard
In C# 12, what feature allows creating parameterless struct constructors with field initializers?
Answer: B
C# 12 allows field initializers directly in struct declarations, enabling parameterless constructors with initialized default values.
Q.9Hard
What is the correct way to implement the Dispose pattern in C# for resource cleanup?
Answer: A
The proper Dispose pattern implements IDisposable and uses a finalizer/destructor as a safety net, following the recommended pattern in C#.
Q.10Hard
A company develops a payment system using multiple payment methods (Credit Card, UPI, Wallet). Which OOP principle best applies here?
Answer: B
Creating an IPayment interface with different payment classes implementing it demonstrates polymorphism, allowing the system to handle various payment methods uniformly.
Q.11Hard
Consider a scenario where class C inherits from B, and B inherits from A. If all three have a method Display(), what does calling Display() on C object invoke?
Answer: C
Due to method overriding and polymorphism, C's Display() executes if overridden. Otherwise, the most derived available implementation in the inheritance chain executes.
Q.12Hard
In C# 10+, what is the purpose of the 'file-scoped type' modifier?
Answer: A
The 'file' access modifier (C# 11+) restricts type visibility to the file it's declared in, improving encapsulation at the file level.
Q.13Hard
A developer needs to ensure that a method in a derived class cannot be overridden further. Which keyword should be used?
Answer: B
'sealed override' prevents further overriding of an already overridden virtual method in derived-from-derived classes.
Q.14Hard
In C#, when implementing IDisposable pattern, what should a derived class do if the base class also implements it?
Answer: B
Derived classes must call base.Dispose() to ensure proper cleanup of base class resources, following the disposable pattern correctly.
Q.15Hard
What is covariance in C# generics and interfaces?
Answer: B
Covariance (using 'out' keyword) allows a more derived type to be returned where a base type is expected, improving type safety and flexibility.
Q.16Hard
A logging system needs to handle multiple logger types (File, Database, Console) interchangeably. Which design pattern combined with polymorphism best suits this?
Answer: B
Strategy pattern defines a family of algorithms (logger types) and makes them interchangeable through interface polymorphism, allowing runtime selection.
Q.17Hard
What will happen if you try to override a non-virtual method in a derived class in C#?
Answer: B
In C#, if you attempt to override a non-virtual method without using 'new' keyword, the compiler issues a warning but the method is hidden (not overridden). Using 'new' keyword explicitly hides the base method.
Q.18Hard
A banking application uses inheritance where Account is the parent class and SavingsAccount, CurrentAccount are derived classes. Each has different withdrawal rules. Which design principle is being followed?
Answer: B
Open/Closed Principle states that classes should be open for extension (SavingsAccount, CurrentAccount extend Account) but closed for modification (Account class rules remain unchanged). This is a classic example of OCP.
Q.19Hard
In a hospital management system, Doctor and Nurse classes need to perform a common Logout() operation but have different Login() procedures. What is the best OOP approach?
Answer: A
A base class with virtual Logout() allows common implementation, while abstract Login() forces derived classes to provide their own implementation. This combines code reuse with flexibility.
Q.20Hard
A gaming application needs to implement different weapons (Sword, Gun, Bow) with different attack methods. Which approach is most maintainable?
Answer: A
Inheritance and polymorphism provide better maintainability, scalability, and follow SOLID principles compared to conditional logic.