Home Subjects C# Programming OOP in C#

C# Programming
OOP in C#

C# and .NET for campus placement

51 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 51
Topics in C# Programming
Q.21 Medium OOP in C#
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();
A C B A
B A B C
C B A C
D A C B
Correct Answer:  B. A B C
EXPLANATION

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.

Test
Q.22 Medium OOP in C#
A developer implements IComparable interface in a Student class. What must the class implement?
A CompareTo() method only
B Equals() and GetHashCode() methods only
C ToString() method only
D All abstract and virtual methods of the interface
Correct Answer:  A. CompareTo() method only
EXPLANATION

IComparable interface requires implementing the CompareTo() method, which defines how objects of the class should be compared for sorting purposes.

Test
Q.23 Medium OOP in C#
An application needs to define behavior that multiple unrelated classes must follow. Which feature should be used?
A Abstract Class
B Interface
C Base Class with virtual methods
D Sealed Class
Correct Answer:  B. Interface
EXPLANATION

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.

Test
Q.24 Medium OOP in C#
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;
A Constructor Destructor
B Constructor (Destructor runs later during garbage collection)
C Only Constructor
D Compilation Error
Correct Answer:  B. Constructor (Destructor runs later during garbage collection)
EXPLANATION

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.

Test
Q.25 Medium OOP in C#
What is the difference between a class and a struct in C#?
A Classes are value types, structs are reference types
B Classes are reference types, structs are value types
C Both are the same, just different naming conventions
D Structs cannot have methods, only classes can
Correct Answer:  B. Classes are reference types, structs are value types
EXPLANATION

Classes are reference types (stored on heap), while structs are value types (stored on stack). This affects memory management and performance characteristics differently.

Test
Q.26 Medium OOP in C#
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?
A Encapsulation
B Inheritance
C Polymorphism
D Abstraction
Correct Answer:  C. Polymorphism
EXPLANATION

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.

Test
Q.27 Medium OOP in C#
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();
A Base
B Derived
C Compilation Error
D Runtime Error
Correct Answer:  B. Derived
EXPLANATION

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'.

Test
Q.28 Medium OOP in C#
In C# 13+ (projected features), nullable reference types help prevent which type of error?
A Stack overflow
B Null reference exceptions (NullReferenceException)
C Memory leaks
D Type casting errors
Correct Answer:  B. Null reference exceptions (NullReferenceException)
EXPLANATION

Nullable reference types (enabled from C# 8) provide compile-time checks for null references, helping prevent NullReferenceException at runtime.

Test
Q.29 Medium OOP in C#
In C#, what is the difference between 'is' and 'as' operators?
A 'is' checks type, 'as' converts type and returns null if incompatible
B 'as' checks type, 'is' converts type
C Both are identical
D 'is' throws exception, 'as' returns null
Correct Answer:  A. 'is' checks type, 'as' converts type and returns null if incompatible
EXPLANATION

The 'is' operator returns a boolean indicating type compatibility. The 'as' operator performs type conversion and returns null if the conversion fails (no exception).

Test
Q.30 Medium OOP in C#
An abstract class Shape has an abstract method CalculateArea(). Which statement is true?
A Shape can be instantiated directly
B Shape cannot be instantiated; derived classes must implement CalculateArea()
C Abstract methods are optional to override
D Abstract classes cannot have concrete methods
Correct Answer:  B. Shape cannot be instantiated; derived classes must implement CalculateArea()
EXPLANATION

Abstract classes cannot be instantiated. Derived classes must provide concrete implementations of all abstract methods, enforcing a contract.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips