InterviewSolution
| 1. |
In the below Java Program, how many objects are eligible for garbage collection? |
|
Answer» class Main{ public STATIC void main(String[] args){ int[][] num = new int[3][]; num[0] = new int[5]; num[1] = new int[2]; num[2] = new int[3]; num[2] = new int[5]; num[0] = new int[4]; num[1] = new int[3]; num = new int[2][]; }} In the above program, a total of 7 OBJECTS will be eligible for garbage collection. Let’s visually understand what's happening in the code. In the above figure on line 3, we can see that on each array index we are DECLARING a new array so the reference will be of that new array on all the 3 indexes. So the old array will be pointed to by none. So these three are eligible for garbage collection. And on line 4, we are creating a new array object on the older reference. So that will point to a new array and older multidimensional objects will become eligible for garbage collection. |
|