A developer creates a class Library and another class Book. The Library class contains multiple Book objects. What is this relationship called?
Answer: B
Composition represents a 'has-a' relationship where one class contains objects of another class. Here, Library 'has-a' Book collection.
Q.42Medium
In C# 9+, which feature allows you to create immutable reference types?
Answer: B
Records (introduced in C# 9) are reference types designed for immutability with built-in equality comparison and ToString() methods.
Q.43Medium
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.44Medium
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.45Medium
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).
Advertisement
Q.46Hard
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.47Hard
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.48Hard
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.49Hard
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.50Hard
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.51Hard
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.52Hard
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.53Medium
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.54Easy
Which of the following best describes encapsulation in C#?
Answer: A
Encapsulation is the bundling of data (variables) and methods (functions) into a single unit (class) while hiding the internal details. Access modifiers like private, protected, and public control visibility.
Q.55Medium
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'.
Q.56Medium
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.57Medium
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.58Medium
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.59Medium
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.60Easy
What is the purpose of the 'base' keyword in C#?
Answer: B
The 'base' keyword allows a derived class to access members (methods, properties, constructors) of the parent class. It's commonly used to call the parent constructor or override methods while keeping parent functionality.