What happens when a derived class constructor does not explicitly call the base class constructor in C#?
Answer: B
C# automatically calls the parameterless constructor of the base class if no explicit base() call is made, ensuring base class initialization.
Q.202Medium
In a company payroll system, Employee is the base class with CalculateSalary() as virtual method. Manager and Intern classes override it differently. Why is this design preferred?
Answer: B
This design enables polymorphism, allowing different employee types to calculate salaries differently while using the same interface, improving maintainability.
Q.203Medium
Which statement about abstract classes in C# is INCORRECT?
Answer: D
Abstract classes can have properties, fields, concrete methods, and abstract members. The statement that they cannot have properties is incorrect.
Q.204Hard
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.
Q.205Hard
In a complex system where a class needs to inherit from multiple sources of functionality while maintaining a single base type, which C# feature is most appropriate?
Answer: B
C# doesn't support multiple inheritance of classes, but allows one base class and multiple interfaces, providing flexibility while maintaining hierarchy.
Advertisement
Q.206Hard
A financial system implements IEquatable<Account> for comparing accounts. What is the primary benefit of implementing this interface?
Answer: B
IEquatable<T> provides type-safe equality comparison, improves performance by avoiding boxing, and is essential for proper behavior in collections like Dictionary.
Q.207Hard
In a real estate system, consider Property (base) with House and Apartment (derived). If Property has a sealed method CalculateTax(), what happens in House class?
Answer: B
The sealed keyword on a virtual method prevents overriding in derived classes, ensuring that implementation is locked in the current class.
Q.208Medium
What is the output of the following C# code?
class Base { public virtual void Display() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Display() { Console.WriteLine("Derived"); } }
Base obj = new Derived();
obj.Display();
Answer: B
Due to polymorphism and method overriding, the Derived class's Display() method is called even though the reference type is Base. This demonstrates runtime polymorphism.
Q.209Easy
In C#, what is the primary purpose of using an interface?
Answer: B
Interfaces in C# define a set of method and property declarations that implementing classes must provide, establishing a contract without implementation details.
Q.210Medium
Which keyword in C# is used to prevent a class from being inherited?
Answer: C
The 'sealed' keyword prevents a class from being used as a base class for inheritance, ensuring that derived classes cannot override the sealed class.
Q.211Medium
What will be the result of the following C# code?
class Animal { }
class Dog : Animal { }
Dog dog = new Animal(); // Line 1
Answer: B
You cannot assign a base class instance to a derived class reference without explicit casting. This violates type safety rules in C#.
Q.212Medium
Which of the following correctly demonstrates composition in C#?
Answer: B
Composition is represented by having an object of one class as a member variable of another class, allowing code reuse through object containment rather than inheritance.
Q.213Medium
In a banking application, you need to create a base class that cannot be instantiated but defines common properties for SavingsAccount and CheckingAccount. Which should you use?
Answer: C
An abstract class is ideal when you want to provide common functionality and properties to derived classes while preventing direct instantiation. It can have both abstract and concrete members.
Q.214Medium
In a logistics system, both Truck and Bicycle need to calculate delivery cost. Instead of duplicating code, you decide to use an interface. What advantage does this provide?
Answer: A
Interfaces establish a contract that forces implementing classes to provide their own logic for calculating delivery costs, promoting code consistency without duplicating implementation.
Q.215Easy
Which statement is TRUE about abstract methods in C#?
Answer: B
Abstract methods have no implementation in the abstract class and force derived classes to provide their own implementation. They cannot be private as they must be overridable.
Q.216Hard
Consider a scenario where class X implements interfaces IA and IB, and both interfaces have a method named Process(). How should X implement this?
Answer: B
C# allows one implementation to satisfy multiple interfaces when they have the same method signature. Explicit interface implementation can be used if methods need different behaviors.
Q.217Hard
In a healthcare system, Doctor is a derived class from Employee. If you want to ensure that Doctor's constructor calls Employee's constructor before executing, which approach is correct?
Answer: A
The 'base' keyword in the constructor initialization list ensures the parent class constructor is called before the derived class constructor body executes, maintaining proper initialization order.
Q.218Medium
In a real-time inventory management system, a Product class needs to track stock levels that should not be modified directly from outside the class. Which OOP principle should be applied here, and how?
Answer: A
Encapsulation is the principle of bundling data and methods together while hiding internal details. Using private fields with public properties (getters/setters) allows controlled access to the stock level, preventing unauthorized direct modifications. This is the standard practice in inventory systems.
Q.219Easy
Consider a payment processing system where PaymentProcessor is an abstract class with an abstract method ProcessPayment(). CreditCardProcessor and UPIProcessor are derived classes. What will happen if you try to instantiate PaymentProcessor directly in C#?
Answer: B
Abstract classes in C# cannot be instantiated directly. The compiler enforces this rule at compile-time. Abstract classes serve as blueprints and must be inherited by concrete classes that provide implementations for all abstract members. This ensures proper design and forces derived classes to implement required functionality.
Q.220Easy
Which namespace in C# contains the collections like List<T>, Dictionary<K,V>, and Queue<T>?
Answer: A
System.Collections.Generic is the standard namespace for generic collections in C#. It provides type-safe collections that prevent runtime errors.