MCQs
shuffle - randomizes all the elements in a list.
None.
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];
}
}
Where will be the most chance of the garbage collector being invoked?
Option 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.
Option 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].
Option B is wrong. Because it is only one of the references to the object obj1, the other reference
is maintained in obj2[0].
Option 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].
None.
this keyword can be used inside any method to refer to the current object. this is always a
reference to the object on which the method was invoked.
None.
None.
java.lang.StringBuffer is the only class in the list that uses the default methods provided by
class Object.
Collection interfaces defines core methods that all the collections like set,
Queue is not a part of collection framework.