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.82Medium
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.83Medium
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.84Medium
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.85Medium
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.
Advertisement
Q.86Medium
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.87Medium
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.88Medium
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.89Medium
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.90Medium
In ASP.NET, what is the purpose of the Global.asax file?
Answer: A
Global.asax handles application-level events like Application_Start, Application_End, and Session_Start.
Q.91Medium
Which attribute is used to mark a method as an HTTP endpoint in ASP.NET Core?
Answer: B
[HttpGet], [HttpPost], [HttpPut], [HttpDelete] attributes define HTTP methods for controller actions in ASP.NET Core.
Q.92Medium
What is the difference between ASP.NET Framework and ASP.NET Core?
Answer: A
ASP.NET Core is cross-platform (Windows, Linux, macOS) and open-source, while ASP.NET Framework is Windows-only.
Q.93Medium
Which of the following is NOT a built-in state management technique in ASP.NET?
Answer: D
ViewState, Session State, Application State, and Cookies are valid state management techniques. Block State is not a standard ASP.NET technique.
Q.94Medium
What is the role of Middleware in ASP.NET Core?
Answer: A
Middleware components handle cross-cutting concerns like authentication, logging, and exception handling in the request pipeline.
Q.95Medium
In an ASP.NET Core application, where is Dependency Injection configured?
Answer: A
Dependency Injection is configured in the ConfigureServices method of Startup.cs (or Program.cs in newer versions).
Q.96Medium
What is the purpose of the [ValidateAntiForgeryToken] attribute in ASP.NET MVC?
Answer: A
[ValidateAntiForgeryToken] verifies that form submissions come from your application, preventing CSRF attacks.
Q.97Medium
Which of the following methods is used to pass data from Controller to View in ASP.NET MVC?
Answer: A
ViewBag (dynamic) and ViewData (dictionary) are common ways to pass data from Controller to View in ASP.NET MVC.
Q.98Medium
What is Entity Framework in ASP.NET context?
Answer: A
Entity Framework is Microsoft's ORM that allows developers to work with databases using .NET objects instead of raw SQL.
Q.99Medium
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.100Medium
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.