• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&S - Practice Exam 3 - Q28

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic