After Line 12 , how many objects are eligible for garbage collection?
The answer is one object.
It is better to explain with the diagram and its really very helpful to me . Actually they told that we have to draw a small picture everytime whenever we see this type questions in the real exam too..
Actually they told that we have to draw a small picture everytime whenever we see this type questions in the real exam too
Who they?? You are not obliged to draw a diagram for these type of questions. If you can figure out the answer without any diagram, then you don't need to draw a diagram. If I have to create a diagram for this question, it would be like this
9. A a1=null; A reference of class A is created an initialized to null.
10. A a2=new A(new A(null)); Here a2 points to an Object of class A. The class A has a field named a and the instance of class A which we pass to the constructor will be set to the field a. We can split this line to understand it better
11. A a3=new A(a2); This one is also not that tough. a3 points to an instance of A and a3.a points to a2.
The rest of the statements should be easy to understand I think...