A student management system implements IComparable<Student> to sort students. Which method must be implemented?
Answer: B
IComparable<T> interface requires implementation of CompareTo method to define comparison logic between objects of the same type.
Q.42Medium
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.43Medium
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.44Medium
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.45Medium
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.
Advertisement
Q.46Medium
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.47Medium
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.48Medium
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.49Medium
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.50Medium
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.51Medium
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.