Users Online

· Guests Online: 43

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Questions & Answers ? Destructors in Class

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

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

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 the 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 There is one common garbage collector for all programs & To garbage collect an object set all references to it as null

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

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
Explanation: None.

 

 

 

 

7. What will be the output of the following C# 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 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

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

Answer: d
Explanation: None.
Output:

 

180.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.18 seconds
10,843,996 unique visits