Java OCA OCP Practice Question 1428

Question

How many objects are eligible for garbage collection immediately before the end of the main() method?

public class Main { 
   public static void main(String[] v) { 
      String[] b = new String[1]; 
      int[] scores = new int[1]; 
      b = null; 
      scores = null; 
   } 
} 
  • A. None
  • B. One
  • C. Two
  • D. Three
  • E. Four


C.

Note

All arrays are objects regardless of whether they point to primitives or classes.

That means both b and scores are objects.

Both are set to null so they are eligible for garbage collection.

The b array is initialized to all null references.

There are no objects inside.

The scores array is initialized to all 0 values.

Only two objects exist to be eligible for garbage collection, and Option C is correct.




PreviousNext

Related