A developer implements IComparable interface in a Student class. What must the class implement?
Answer: A
IComparable interface requires implementing the CompareTo() method, which defines how objects of the class should be compared for sorting purposes.
Q.62Hard
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.63Hard
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.64Medium
What is the correct order of constructor execution in multilevel inheritance?
class A { public A() { Console.WriteLine("A"); } }
class B : A { public B() { Console.WriteLine("B"); } }
class C : B { public C() { Console.WriteLine("C"); } }
new C();
Answer: B
Constructor execution follows from parent to child. When C() is called, it implicitly calls B() which calls A(). So output is: A B C. Base class constructors execute before derived class constructors.
Q.65Medium
An e-commerce system needs to ensure that a Product class cannot be instantiated directly but can be inherited. Additionally, it must define some concrete methods and some abstract methods. Which approach is best?
Answer: C
Abstract classes can have both concrete (implemented) and abstract (unimplemented) methods. They cannot be instantiated directly but can be inherited. This perfectly fits the requirement.
Advertisement
Q.66Easy
What is the output of this C# code?
public class Employee
{
private string name = "John";
public Employee(string n) { name = n; }
}
Employee emp = new Employee("Alice");
Console.WriteLine(emp.name);
Answer: C
The 'name' field is declared as 'private', so it cannot be accessed outside the class. Attempting to access emp.name from outside the class results in a compilation error.
Q.67Medium
A payment processing system implements multiple interfaces: IPayment, IRefundable, IAuditTrail. A PaymentProcessor class must implement all three. Which statement is true?
Answer: C
C# supports multiple interface implementation. A class can implement multiple interfaces separated by commas, and must implement all abstract members of each interface.
Q.68Easy
Which of the following best describes the 'this' keyword in C#?
Answer: B
'this' keyword represents the current instance of the class. It is used to access instance members, differentiate between local and instance variables, and pass the current object as a parameter.
Q.69Hard
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.70Easy
What is the primary purpose of encapsulation in OOP?
Answer: B
Encapsulation bundles data and methods together, hiding internal implementation and controlling access through public interfaces, improving maintainability and security.
Q.71Easy
Which keyword is used to create a class that cannot be instantiated in C#?
Answer: B
The 'abstract' keyword prevents instantiation of a class and forces derived classes to implement abstract members, establishing a contract for subclasses.
Q.72Medium
In a library management system, multiple types of items (Book, Magazine, DVD) share common properties like ID and Title. Which OOP principle should be applied here?
Answer: B
Inheritance allows creating a base class (LibraryItem) with common properties, and derived classes can inherit these properties, reducing code duplication.
Q.73Medium
What is the difference between 'sealed' and 'abstract' keywords in C#?
Answer: A
sealed prevents a class from being inherited, while abstract requires derived classes to implement its abstract members. They serve opposite purposes.
Q.74Medium
A vehicle management system requires different vehicle types (Car, Bike, Truck) to calculate toll fees differently. Which feature should be used?
Answer: C
Polymorphism through inheritance allows each vehicle type to override the toll calculation method with its own implementation, enabling different behavior for each type.
Q.75Easy
In C#, what is the correct syntax to implement multiple interfaces in a class?
Answer: A
C# uses colon (:) to implement interfaces, and multiple interfaces are separated by commas. The 'implements' keyword is used in Java, not C#.
Q.76Medium
Which of the following statements about virtual methods in C# is TRUE?
Answer: B
Virtual methods are designed to be overridden by derived classes, enabling polymorphic behavior and runtime method resolution.
Q.77Medium
A restaurant ordering system needs to ensure each order has a unique ID generated automatically. Which design pattern and feature should be used?
Answer: A
Static variables are shared across all instances and can be used to generate unique IDs automatically without modifying external data.
Q.78Medium
What is the output of the following C# code?
public class Animal { public virtual void Sound() { Console.WriteLine("Some sound"); } }
public class Dog : Animal { public override void Sound() { Console.WriteLine("Bark"); } }
Animal animal = new Dog();
animal.Sound();
Answer: B
Due to polymorphism, the overridden method in Dog class is called at runtime, printing 'Bark' even though the reference type is Animal.
Q.79Medium
In an inventory management system, a Product class has a Price property. Which approach best ensures price cannot be negative?
Answer: B
Properties with private backing fields allow validation in setters, ensuring data integrity by preventing invalid values like negative prices.
Q.80Easy
Which of the following is NOT a pillar of OOP in C#?
Answer: D
The four pillars of OOP are Inheritance, Polymorphism, Encapsulation, and Abstraction. Compilation is a language feature, not an OOP principle.