Sample questions
Question 1Medium
after line 11 runs, how many objects are eligible for garbage collection?
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
- 0
- 1
- 2
- 3
Answer: C. 2
Explanation
This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.
Step-by-step solution
- After line 6, object A is held by x2; after line 7, object B is held by x3.
- Lines 8 and 9 make A.x point to B and B.x point to A, so they refer to each other.
- Line 10 points x2 at a new object C, and line 11 points x3 at C too.
- A and B are reachable only from each other, not from any live variable, so 2 objects are eligible for garbage collection: option C.
Why the other options are wrong
- A. A and B cannot be reached from any live variable, so some objects are eligible.
- B. Both A and B are unreachable, not just one.
- D. C is still referenced by x2 and x3, so it is not eligible.
Tip: Objects that only refer to each other, with no live variable pointing in, are eligible for garbage collection.
Question 2Medium
After line 8 runs. how many objects are eligible for garbage collection?
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
- 0
- 1
- 2
- 3
Answer: B. 1
Explanation
By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.
Step-by-step solution
- Line 1 creates object A, and x refers to it.
- M1 creates object B, which is returned and stored in x2.
- Line 7 creates object C, and x4 refers to it. Line 8 sets x2 to C.
- Object B now has no reference, so it is eligible for garbage collection.
- Object A is still held by x and object C by x4 and x2, so the count is 1, option B.
Why the other options are wrong
- A. Object B loses its last reference at line 8, so the count is not 0.
- C. Only B is unreferenced; A and C are still referenced.
- D. Only one object becomes unreachable.
Tip: An object is garbage once no reference points to it; reassigning a method parameter does not change the caller's variable.
Question 3Medium
- All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
- Objects with at least one reference will never be garbage collected.
- Objects from a class with the
finalize() method overridden will never be garbage collected. - Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
Answer: D. Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
Explanation
All objects are placed in the garbage collectible heap.
A is incorrect because the garbage collector makes no guarantees.
B is incorrect because islands of isolated objects can exist.
C is incorrect because finalize() has no such mystical powers.
Step-by-step solution
- Option A is false: the garbage collector makes no guarantee that every eligible object is collected.
- Option B is false: an object can be collected when only other unreachable objects refer to it.
- Option C is false: overriding finalize() does not stop an object from being collected.
- Option D is true: objects, including those made in anonymous inner classes, are placed on the garbage-collectible heap, so it is the correct answer.
Why the other options are wrong
- A. This is false because the garbage collector makes no guarantee about timing or collection.
- B. This is false because unreachable objects can be collected even if other objects still refer to them.
- C. This is false because finalize() has no power to stop collection.
Tip: Garbage collection depends on reachability; no code can force or guarantee when an object is collected.
Question 4Medium
When is the Demo object eligible for garbage collection?
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
- After line 7
- After line 8
- After the
start() method completes - When the instance running this code is made eligible for garbage collection.
Answer: D. When the instance running this code is made eligible for garbage collection.
Explanation
D is correct. By a process of elimination.
A is wrong. The variable d is a member of the Test class and is never directly set to null.
B is wrong. A copy of the variable d is set to null and not the actual variable d.
C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.
Step-by-step solution
- start() creates a Demo object and stores it in the field d.
- TakeDemo sets only its own parameter copy to null and then to a new Demo, so the field d is unchanged.
- The field d still refers to the first Demo while the Test object is reachable.
- The Demo becomes eligible for garbage collection only when the Test instance is unreachable, so option D is correct.
Why the other options are wrong
- A. Line 7 only passes the reference; the field d still holds the Demo.
- B. Line 8 is the end of start(); the field d still refers to the Demo.
- C. When start() ends, the field d still refers to the Demo object.
Tip: An object stays reachable while any reachable field, variable or static points to it; changing a parameter does not change the caller's reference.
Question 5Medium
Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
- delete
- free
- new
- None of the mentioned
Answer: D. None of the mentioned
Explanation
Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
Step-by-step solution
- Java has no delete or free operator.
- The new operator allocates memory; it does not free it.
- Java frees objects that are no longer reachable through garbage collection, done by the runtime.
- None of the listed operators does this, so option D is the answer.
Why the other options are wrong
- A. Delete is a C++ operator, and Java does not have it.
- B. Free is a C function, not a Java operator.
- C. New creates objects; it does not free memory.
Tip: Java has no manual free: garbage collection reclaims unreachable objects.
Question 6Medium
At what point is the Bar object, created on line 6, eligible for garbage collection?
class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}
- after line 12
- after line 14
- after line 7, when
doBar() completes - after line 15, when
main() completes
Answer: B. after line 14
Explanation
B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14.
A is wrong. This actually protects the object from garbage collection.
C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6.
D is wrong. Not applicable because the object is eligible for garbage collection after line 14.
Step-by-step solution
- Line 6 creates the Bar object. doBar returns it, so main's newBar refers to it.
- Line 14 assigns a new Bar to newBar. The first object then has no references left.
- So the first Bar object becomes eligible for garbage collection after line 14.
Why the other options are wrong
- A. NewBar still refers to the object at line 12 and until line 14 runs.
- C. DoBar returns the object to main, and main still holds it.
- D. The last reference is removed at line 14, well before main completes.
Tip: An object is eligible for garbage collection once no reference to it can be reached.
Question 7Medium
What allows the programmer to destroy an object x?
x.delete()x.finalize()Runtime.getRuntime().gc()- Only the garbage collection system can destroy an object.
Answer: D. Only the garbage collection system can destroy an object.
Explanation
Java does not let the programmer destroy an object directly. Only the garbage collector reclaims an object, and only after nothing refers to it. finalize() and Runtime.gc() do not destroy the object, and objects have no delete() method.
Step-by-step solution
- Object x can only be destroyed by garbage collection when no longer referenced.
- delete() and finalize() methods do not destroy objects but perform other tasks.
- gc() suggests garbage collection but does not directly destroy objects.
- Only the garbage collection system has authority to destroy objects.
- Final answer: Only the garbage collection system can destroy an object.
Why the other options are wrong
- A. delete() is not a method of Java objects, so calling x.delete() does not destroy anything.
- B. finalize() only runs cleanup code before collection; the programmer cannot force the object to be destroyed.
- C. gc() only requests garbage collection; it does not destroy object x directly, and the JVM may ignore it.
Tip: Remember: Only garbage collection can destroy objects in Java.
Question 8Medium
Which function is used to perform some action when the object is to be destroyed?
- finalize()
- delete()
- main()
- close()
Answer: A. finalize()
Explanation
finalize method in java is a special method much like main method in java. finalize() is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing connection if open etc. Main issue with finalize method in java is its not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example an object may wait indefinitely after becoming eligible for garbage collection and before its finalize() method gets called.
Step-by-step solution
- finalize() is a special method in Java called before garbage collector reclaims the object.
- It gives the object one last chance to perform cleanup tasks like closing connections or releasing resources.
- delete() is not a valid method in Java for object destruction.
- main() is the entry point of a Java application, not related to object destruction.
- close() is used for streams or connections, not for destroying objects.
- Therefore, finalize() is the correct function for performing actions when an object is destroyed.
Why the other options are wrong
- B. There is no delete() method in Java; objects are removed by garbage collection, not by calling delete().
- C. main() is the program entry point where execution starts, not a method that runs when an object is destroyed.
- D. close() only releases streams or connections, and it does not destroy the object itself.
Tip: Remember, finalize() is used for cleanup before garbage collection, not for explicit object destruction.
Question 9Medium
Where will be the most chance of the garbage collector being invoked?
class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
- After line 9
- After line 10
- After line 11
- Garbage collector never invoked in
methodA()
Answer: D. Garbage collector never invoked in methodA()
Explanation
D is correct. Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6.
A is wrong. Because the reference to obj1 is stored in obj2[0]. The Object obj1 still exists on the heap and can be accessed by an active thread through the reference stored in obj2[0].
B is wrong. Because it is only one of the references to the object obj1, the other reference is maintained in obj2[0].
C is wrong. The garbage collector will not be called here because a reference to the object is being maintained and returned in obj2[0].
Step-by-step solution
- Inside methodA, obj1 and obj2 both refer to objects, and methodA is still running.
- After obj1 = null, the object is still reachable through obj2[0], so it is not garbage.
- Nothing inside methodA makes any object unreachable while the method is running.
- The collector can only free these objects after methodA returns and the reference is dropped. Option D is the answer.
Why the other options are wrong
- A. This point is inside methodA, where the object is still reachable through obj2[0].
- B. After obj1 = null, the object is still reachable through obj2[0].
- C. Obj1 still refers to the object at this point, so it cannot be freed.
Tip: An object is eligible for garbage collection only when no live reference path leads to it.
Question 10Medium
When is the Float object, created in line 3, eligible for garbage collection?
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[1];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
- just after line 5
- just after line 6
- just after line 7
- just after line 8
Answer: C. just after line 7
Explanation
A is wrong. This simply copies the object reference into the array.
B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to the Float object.
C is correct. The thread of execution will then not have access to the object.
Step-by-step solution
- Line 5 stores the Float reference in the array, so the object is still reachable.
- Line 6 sets o to null, but oa[0] still refers to the Float object.
- Line 7 sets oa[0] to null, so no variable or array element refers to the Float object.
- From just after line 7 the object cannot be reached, so it is eligible for garbage collection, which is option C.
Why the other options are wrong
- A. Line 5 only copies the reference into the array, and o still refers to the object.
- B. O is null, but the array element oa[0] still refers to the Float object.
- D. The object is already unreachable after line 7; line 8 is too late, since it returns null.
Tip: An object becomes eligible for garbage collection once no reference path leads to it, so clear every reference, including array elements.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →