What is the output of the following code?
class Animal { public virtual void Sound() { Console.WriteLine("Generic Sound"); } }
class Dog : Animal { public override void Sound() { Console.WriteLine("Bark"); } }
Dog dog = new Dog();
dog.Sound();
Answer: B
Polymorphism ensures that the overridden method in Dog class executes. The override keyword allows the derived class to provide its specific implementation.
Q.22Medium
An abstract class Shape has an abstract method CalculateArea(). Which statement is true?
Answer: B
Abstract classes cannot be instantiated. Derived classes must provide concrete implementations of all abstract methods, enforcing a contract.
Q.23Medium
In C#, what is the difference between 'is' and 'as' operators?
Answer: A
The 'is' operator returns a boolean indicating type compatibility. The 'as' operator performs type conversion and returns null if the conversion fails (no exception).
Q.24Medium
In C# 13+ (projected features), nullable reference types help prevent which type of error?
Answer: B
Nullable reference types (enabled from C# 8) provide compile-time checks for null references, helping prevent NullReferenceException at runtime.
Q.25Medium
What is the output of the following C# code?
public class Base
{
public virtual void Display() { Console.WriteLine("Base"); }
}
public class Derived : Base
{
public override void Display() { Console.WriteLine("Derived"); }
}
Base obj = new Derived();
obj.Display();
Answer: B
This demonstrates polymorphism. Even though the reference is of type Base, the actual object is Derived. The overridden method in Derived class is called, printing 'Derived'.
Advertisement
Q.26Medium
A developer creates an abstract class Account with an abstract method CalculateInterest(). Two derived classes SavingsAccount and CurrentAccount implement this method differently. This scenario best demonstrates which OOP principle?
Answer: C
This is polymorphism - the ability of different derived classes to override the same abstract method with different implementations. While abstraction is also involved, polymorphism is the primary principle being demonstrated.
Q.27Medium
What is the difference between a class and a struct in C#?
Answer: B
Classes are reference types (stored on heap), while structs are value types (stored on stack). This affects memory management and performance characteristics differently.
Q.28Medium
In C#, what will be the output of the following code?
class Test
{
public Test() { Console.WriteLine("Constructor"); }
~Test() { Console.WriteLine("Destructor"); }
}
Test t = new Test();
t = null;
Answer: B
The constructor runs immediately when the object is created. The destructor (finalizer) is called by the garbage collector when the object is destroyed, which happens later, not immediately when t = null.
Q.29Medium
An application needs to define behavior that multiple unrelated classes must follow. Which feature should be used?
Answer: B
Interfaces define contracts that unrelated classes can implement. If classes have common inheritance relationship, abstract classes are better. Interfaces support multiple implementation, making them ideal for unrelated classes.
Q.30Medium
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.31Medium
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.32Medium
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.
Q.33Medium
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.34Medium
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.35Medium
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.36Medium
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.37Medium
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.38Medium
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.39Medium
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.40Medium
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.