aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&S - Practice Exam 3 - Q28 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&S - Practice Exam 3 - Q28" Watch "K&S - Practice Exam 3 - Q28" New topic
Author

K&S - Practice Exam 3 - Q28

Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
Hi guys,

I have a problem with this question:



When line 8 is reached, which are true? (Choose all that apply.)

A. a1.acctNum == a3.password
B. a1.password == a2.password
C. Three objects are eligible for garbage collection.
D. Four objects are eligible for garbage collection.
E. Six objects are eligible for garbage collection.
F. Less than three objects are eligible for garbage collection.
G. More than six objects are eligible for garbage collection.

And this is the answer in the book:

Answer (for Objective 7.4):
- B and D are correct. B is correct, although when line 8 is reached, the references are lost.
D is correct because only one Account object is created, and a1, a2, and a3 all refer to it.
The Account object has two Long objects associated with it, and finally the anonymous
Banker object is also eligible.
- A, C, E, F, and G are incorrect based on the above.


Option B is difficult to understand. When line 8 is reached all references are lost. Well, I will believe this because I think the author wanted to ask if both 'password' references point to the same object in the heap.

My worry is with option D. I think there are 5 objects eligible for garbage collection and not 4. The Account object, one Long created in line 12 using constructor Long(String) (the String is in the pool, so is reachable in line 8), one more Long created by boxing in line 15 , another Long created by boxing in line 16 and the Banker object.

What do you think?

Thanks in advance,

Alvaro


OCPJP 6 98%
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Hm, I would agree with you. To make things even more unclear, I think we don't now whether on line 8 that Long object is eligible for gc or even already collected as it is eligible already after execution of line 16.

John
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
We cannot be sure about those objects have been collected or not. It is the JVM which execute the GC process, and maybe this process never happen in our code. Also, if we invoke it (which is not recommended) by System.gc() or Runtime.getRuntime().gc() methods we are not sure if the process will be launched or not.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Alvaro San Millan wrote:
My worry is with option D. I think there are 5 objects eligible for garbage collection and not 4. The Account object, one Long created in line 12 using constructor Long(String) (the String is in the pool, so is reachable in line 8), one more Long created by boxing in line 15 , another Long created by boxing in line 16 and the Banker object.

What do you think?


I agree with you, I think it should be 5 and not 4.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: K&S - Practice Exam 3 - Q28