Home Subjects C# Programming

C# Programming

C# and .NET for campus placement

154 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 71–80 of 154
Topics in C# Programming
Q.71 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.72 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.73 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.74 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.75 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.76 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.77 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
Q.78 Medium OOP in C#
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();
A Generic Sound
B Bark
C Compilation Error
D No Output
Correct Answer:  B. Bark
EXPLANATION

Polymorphism ensures that the overridden method in Dog class executes. The override keyword allows the derived class to provide its specific implementation.

Test
Q.79 Medium OOP in C#
In C# 9+, which feature allows you to create immutable reference types?
A enum
B record
C struct
D interface
Correct Answer:  B. record
EXPLANATION

Records (introduced in C# 9) are reference types designed for immutability with built-in equality comparison and ToString() methods.

Test
Q.80 Medium OOP in C#
A developer creates a class Library and another class Book. The Library class contains multiple Book objects. What is this relationship called?
A Inheritance
B Composition
C Polymorphism
D Abstraction
Correct Answer:  B. Composition
EXPLANATION

Composition represents a 'has-a' relationship where one class contains objects of another class. Here, Library 'has-a' Book collection.

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