C# Questions & Answers on Destructors in Class for Freshers

1. Which operator among the following signifies the destructor operator?
a) ::
b) :
c) ~
d) &
Answer: c
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()
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
Answer: c

Explanation: Definition of destructor.
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 b & c
Answer: d
5. Operator used to free the memory when memory is allocated ?

a) new
b) free
c) delete
d) none of the mentioned
Answer: c

Explanation: ‘New’ is used to allocate memory in the constructors.Hence,we should use ‘delete’ to free that memory.
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
Answer: c
.
7. Output from following set of code ?
  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
Answer: d

Explanation: First constructor ‘sample’ is called and hence then destructor ‘~sample’ is evaluated.
Output : 10.5, 5.5
8. What is the return type of destructor ?

a) int
b) void
c) float
d) none of the mentioned
Answer: d

Explanation: Destructors do not have any return type not even void.
9. What is output for the following set of 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
Answer: c

Explanation: We cannot use any kind of modifier with destructor.
10. Output from selective set of code is :
  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
Answer: d
Output: 180.

Related

Multiple Choice Questions 5409988564098161108

Post a Comment

emo-but-icon

item