C# Questions & Answers – Destructors in Class

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) &
View Answer

Answer: c
Explanation: None.

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()
View Answer

Answer: c
Explanation: Dispose() is only method called by clients of a class to explicitly release any resource like network connection, open files etc. When object is no longer required. Hence, Dispose() provides programmer with such programming control.

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
View Answer

Answer: c
Explanation: Definition of the destructor.
advertisement
advertisement

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
View Answer

Answer: d
Explanation: None.

5. Operator used to free the memory when memory is allocated?
a) new
b) free
c) delete
d) none of the mentioned
View Answer

Answer: c
Explanation: ‘New’ is used to allocate memory in the constructors. Hence, we should use ‘delete’ to free that memory.
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: c
Explanation: None.

7. What will be the output of the following C# code?

advertisement
  1.   class sample
  2.   {
  3.       int i;
  4.       double k;
  5.       public sample (int ii, double kk)
  6.       {
  7.           i = ii;
  8.           k = kk;
  9.           double j = (i) + (k);
  10.           Console.WriteLine(j);
  11.       }
  12.       ~sample()
  13.       {
  14.           double j = i - k;
  15.           Console.WriteLine(j);
  16.       }
  17.   }
  18.   class Program
  19.   {
  20.       static void Main(string[] args)
  21.       {
  22.           sample s = new sample(8, 2.5);
  23.           Console.ReadLine();
  24.       }
  25.   }

a) 0 0
b) 10.5 0
c) Compile time error
d) 10.5 5.5
View Answer

Answer: d
Explanation: First constructor ‘sample’ is called and hence then destructor ‘~sample’ is evaluated.
Output :

advertisement
10.5, 5.5

8. What is the return type of destructor?
a) int
b) void
c) float
d) none of the mentioned
View Answer

Answer: d
Explanation: Destructors do not have any return type not even void.

9. What will be the output of the following C# code?

  1.    class box
  2.    {
  3.        public int volume;
  4.        int width;
  5.        int height;
  6.        int length;
  7.        public box ( int w, int h, int l)
  8.        {
  9.            width = w;
  10.            height = h;
  11.            length = l;
  12.        }
  13.       public ~box()
  14.       {
  15.           volume = width * length * height;
  16.  
  17.       }
  18.  
  19.   }    
  20.   class Program
  21.   {
  22.       static void Main(string[] args)
  23.       {
  24.           box b = new box(4, 5, 9);
  25.           Console.WriteLine(b.volume);
  26.           Console.ReadLine();
  27.       }
  28.  
  29.   }

a) 0
b) 180
c) Compile time error
d) None of the mentioned
View Answer

Answer: c
Explanation: We cannot use any kind of modifier with destructor.

10. What will be the output of the following C# code?

  1.   class box
  2.   {
  3.       public int volume;
  4.       int width;
  5.       int height;
  6.       int length;
  7.       public box ( int w, int h, int l)
  8.       {
  9.           this.width = w;
  10.           this.height = h;
  11.           this.length = l;
  12.       }
  13.       ~ box()
  14.       {
  15.           volume = this.width * this.length * this.height;
  16.           console.writeline(volume);
  17.       }
  18.   }    
  19.   class Program
  20.   {
  21.       public  static void Main(string[] args)
  22.       {
  23.           box b = new box(4, 5, 9);
  24.           Console.ReadLine();
  25.       }
  26.   }

a) 0
b) Code executes successfully but prints nothing
c) Compile time error
d) 180
View Answer

Answer: d
Explanation: None.
Output:

180.

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.