Quiz: Garbage Collection

To View Tricks: Login Required

Number of Questions: 13

Question: 6 -

Where is a new object allocated memory?

Options:
  1. Old space

  2. Young space

  3. JVM

  4. Young or Old space depending on space availability

  5. Answer:

    Young space

    Solution:

    A new object is always created in young space. Once young space is full, a special young collection is run where objects which have lived long enough are moved to old space and memory is freed up in young space for new objects.


Question: 7 -

Which of the following is a garbage collection technique?

Options:
  1. Mark and sweep model

  2. Cleanup model

  3. Space management model

  4. Sweep model

  5. Answer:

    Mark and sweep model

    Solution:

    A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.


Question: 8 -

When is the B object, created in line 3, eligible for garbage collection?

  void start() {
	A a = new A();
	B b = new B();
	a.s(b);
	b = null; /* Line 5 */
	a = null; /* Line 6 */
	System.out.println(""start completed""); /* Line 7 */
}

Options:
  1. after line 7

  2. There is no way to be absolutely certain.

  3. after line 6

  4. after line 5

  5. Answer:

    There is no way to be absolutely certain.

    Solution not available.

Question: 9 -

How many objects are eligible for garbage collection after execution of line ?

  
public class Test
{
	public static void main(String[] args)
	{
		m1(); // Line
	}
	static void m1()
	{
		Test t1 = new Test();
		Test t2 = new Test();
	}
}

Options:
  1. 0

  2. 3

  3. 1

  4. 2

  5. Answer:

    2

    Solution:

    Since t1 and t2 are local objects of m1() method, so they become eligible for garbage collection after complete execution of method unless any of them is returned.


Question: 10 -

Which of the following has the highest memory requirement?

Options:
  1. Heap

  2. Stack

  3. JVM

  4. Class

  5. Answer:

    JVM

    Solution:

    JVM is the super set which contains heap, stack, objects, pointers, etc.