This section of our 1000+ C# multiple choice questions focuses on destructors in class in C# Programming Language.
1. Which operator among the following signifies the destructor operator?
a) ::
b) :
c) ~
d) &
2. The method called by clients of a class to explicitly release any resources like network, connection, open files etc. When the object is no longer required?
a) Finalize()
b) End()
c) Dispose()
d) Close()
3. Name a method which has the same name as that of class and which is used to destroy objects also called automatically when application is finally on process of being getting terminated.
a) Constructor
b) Finalize()
c) Destructor
d) End
4. Which of the following statements are correct?
a) There is one garbage collector per program running in memory
b) There is one common garbage collector for all programs
c) To garbage collect an object set all references to it as null
d) Both There is one common garbage collector for all programs & To garbage collect an object set all references to it as null
5. Operator used to free the memory when memory is allocated?
a) new
b) free
c) delete
d) none of the mentioned
6. Select wrong statement about destructor in C#?
a) A class can have one destructor only
b) Destructors cannot be inherited or overloaded
c) Destructors can have modifiers or parameters
d) All of the mentioned
7. What will be the output of the following C# code?
class sample
{
int i;
double k;
public sample (int ii, double kk)
{
i = ii;
k = kk;
double j = (i) + (k);
Console.WriteLine(j);
}
~sample()
{
double j = i - k;
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample(8, 2.5);
Console.ReadLine();
}
}
a) 0 0
b) 10.5 0
c) Compile time error
d) 10.5 5.5
10.5, 5.5
8. What is the return type of destructor?
a) int
b) void
c) float
d) none of the mentioned
9. What will be the output of the following C# code?
class box
{
public int volume;
int width;
int height;
int length;
public box ( int w, int h, int l)
{
width = w;
height = h;
length = l;
}
public ~box()
{
volume = width * length * height;
}
}
class Program
{
static void Main(string[] args)
{
box b = new box(4, 5, 9);
Console.WriteLine(b.volume);
Console.ReadLine();
}
}
a) 0
b) 180
c) Compile time error
d) None of the mentioned
10. What will be the output of the following C# code?
class box
{
public int volume;
int width;
int height;
int length;
public box ( int w, int h, int l)
{
this.width = w;
this.height = h;
this.length = l;
}
~ box()
{
volume = this.width * this.length * this.height;
console.writeline(volume);
}
}
class Program
{
public static void Main(string[] args)
{
box b = new box(4, 5, 9);
Console.ReadLine();
}
}
a) 0
b) Code executes successfully but prints nothing
c) Compile time error
d) 180
180.