Which of the following statements related to Garbage Collection are correct. Select the two correct answers.

  1. It is possible for a program to free memory at a given time.

  2. Garbage Collection feature of Java ensures that the program never runs out of memory.

  3. It is possible for a program to make an object available for Garbage Collection.

  4. The finalize method of an object is invoked before garbage collection is performed on the object.


Correct Option: C,D

AI Explanation

To answer this question, you need to understand the concept of Garbage Collection. Let's go through each option to understand why it is correct or incorrect:

Option A) It is possible for a program to free memory at a given time.

  • This option is incorrect because in most programming languages with garbage collection, the program does not have direct control over when memory is freed. The garbage collector automatically determines when objects are no longer in use and frees the memory occupied by those objects.

Option B) Garbage Collection feature of Java ensures that the program never runs out of memory.

  • This option is incorrect. While garbage collection helps manage memory, it does not guarantee that a program will never run out of memory. If a program is creating objects faster than the garbage collector can collect them, it can still run out of memory.

Option C) It is possible for a program to make an object available for Garbage Collection.

  • This option is correct. In languages with garbage collection, a program can make an object available for garbage collection by removing all references to that object. Once there are no more references to an object, it becomes eligible for garbage collection.

Option D) The finalize method of an object is invoked before garbage collection is performed on the object.

  • This option is correct. In some programming languages (such as Java), the finalize method of an object is called before the garbage collector collects and frees the memory occupied by the object. The finalize method allows an object to perform any necessary cleanup actions before it is removed from memory.

The correct answers are C and D. These options are correct because a program can make an object available for garbage collection by removing all references to it, and the finalize method is invoked before garbage collection is performed on an object.

Find more quizzes: