Which is true? (Choose all that apply.)

  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

  2. When a stack variable goes out of scope it is eligible for GC.

  3. Some reference variables live on the stack, and some live on the heap.

  4. Only objects that have no reference variables referring to them can be eligible for GC.

  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.


Correct Option: C,E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

This option is incorrect because the finalize() method is not guaranteed to be the last thing that happens before an object is garbage collected. The finalize() method is called by the garbage collector before reclaiming the memory occupied by the object, but the exact timing of when it is called is not specified.

Option B) When a stack variable goes out of scope, it is eligible for GC.

This option is incorrect. Stack variables are automatically deallocated when they go out of scope, but they are not eligible for garbage collection because they are not allocated on the heap. Stack variables are managed by the compiler and deallocated automatically when they go out of scope.

Option C) Some reference variables live on the stack, and some live on the heap.

This option is correct. In Java, reference variables can live on both the stack and the heap. The stack holds local variables and method parameters, while the heap holds objects. Reference variables on the stack store memory addresses pointing to objects on the heap.

Option D) Only objects that have no reference variables referring to them can be eligible for GC.

This option is incorrect. Objects can be eligible for garbage collection even if they have reference variables referring to them, as long as those reference variables are not reachable. The garbage collector determines an object's eligibility for garbage collection based on whether it can be reached through a chain of reference variables from a root object.

Option E) It's possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes.

This option is correct. In Java, you can request garbage collection explicitly using methods provided by the java.lang.Runtime or java.lang.System classes. The System.gc() or Runtime.getRuntime().gc() methods can be used to suggest that the garbage collector should run, but it is ultimately up to the JVM to decide whether to perform garbage collection.

The correct answer is C) Some reference variables live on the stack, and some live on the heap, and E) It's possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes.

Find more quizzes: