Find Correct Statements

Question 1 :

Which statement is true?


A). Programs will not run out of memory.
B). Objects that will never again be used are eligible for garbage collection.
C). Objects that are referred to by other objects will never be garbage collected.
D). Objects that can be reached from a live thread will never be garbage collected.
Answer : Option D

Explanation :

Option D is correct.

Option C is wrong. See the note above on Islands of Isolation (An object is eligible for garbage collection when no live thread can access it - even though there might be references to it).

Option B is wrong. "Never again be used" does not mean that there are no more references to the object.

Option A is wrong. Even though Java applications can run out of memory there another answer supplied that is more right.


Question 2 :

Which statement is true?


A). All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
B). Objects with at least one reference will never be garbage collected.
C). Objects from a class with the finalize() method overridden will never be garbage collected.
D). Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
Answer : Option D

Explanation :

All objects are placed in the garbage collectible heap.

Option A is incorrect because the garbage collector makes no guarantees.

Option B is incorrect because islands of isolated objects can exist.

Option C is incorrect because finalize() has no such mystical powers.


Question 3 :

Which statement is true?


A). Memory is reclaimed by calling Runtime.gc().
B). Objects are not collected if they are accessible from live threads.
C). An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.
D). Objects that have finalize() methods always have their finalize() methods called before the program ends.
Answer : Option B

Explanation :

Option B is correct. If an object can be accessed from a live thread, it can't be garbage collected.

Option A is wrong. Runtime.gc() asks the garbage collector to run, but the garbage collector never makes any guarantees about when it will run or what unreachable objects it will free from memory.

Option C is wrong. The garbage collector runs immediately the system is out of memory before an OutOfMemoryException is thrown by the JVM.

Option D is wrong. If this were the case then the garbage collector would actively hang onto objects until a program finishes - this goes against the purpose of the garbage collector.


Question 4 :

Which statement is true?


A). Calling Runtime.gc() will cause eligible objects to be garbage collected.
B). The garbage collector uses a mark and sweep algorithm.
C). If an object can be accessed from a live thread, it can't be garbage collected.
D). If object 1 refers to object 2, then object 2 can't be garbage collected.
Answer : Option C

Explanation :

This is a great way to think about when objects can be garbage collected.
Option A and B assume guarantees that the garbage collector never makes.
Option D is wrong because of the now famous islands of isolation scenario.