• 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

Que regarding Dan's Exam

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please check this code.



Question:
Which of the following could be a result of attempting to compile and run the program?
a. BaBb, B1, B2, B2
b. B1B2, null, null, Bb
c. , Ba, Bb, Bb
d. BaBbB1B2, null, null, null
e. Ba, B1, B2, Bb
f. Compile-time error
g. Run-time error
h. None of the above

Answer: e

I think it is h. Since none of the objects can be garbage collected and the options does not have , B1, B2, Bb.

Dan quotes:


Inside the body of method m1, the reference to the new instance of class B named Bb is assigned to the static member variable Bc. At that point, the instance of class B named Ba becomes eligible for garbage collection. Method m1 returns a reference to a new instance of class B named B2.



My only doubt is: Isn't the b (argument to m1() ) created each time the m1() is invoked and hence the object B("Ba") will still have b reference and so cannot be gc'ed. (Totally).

Kits,
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Kitty Dayal:
My only doubt is: Isn't the b (argument to m1() ) created each time the m1() is invoked and hence the object B("Ba") will still have b reference and so cannot be gc'ed. (Totally).



I don't know if I understood you question, but
I'll give it a shot.

The parameter b is just used to pass the
reference determined by the argument:
the newly created B object. The parameter
is not maintaining this reference as a
member variable would do, in a way it
forgets what happened as soon as the
method is left.

The static variable bc of type B, in the case
of this exercise, first holds a reference to
Ba. This reference is lost as soon as m1() is
called a second time, because the value is
replaced by the reference to Bb. Therefore
Ba is eligible for GC since there are no
more references to it.

If the variable bc wasn't static and you'd
create an instance of class J before every
call to m1() then Ba would still have it's
reference and would not be eligible for GC.

Cheers,

Gian Franco Casula
[ August 16, 2004: Message edited by: Gian Franco Casula ]
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gian Franco Casula:

Thank you very much.


The parameter b is just used to pass the
reference determined by the argument:
the newly created B object. The parameter
is not maintaining this reference as a
member variable would do, in a way it
forgets what happened as soon as the
method is left.



This answers my doubt. Sorry for not being too clear on my question.

Thanks,
Kits
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Kitty,

the number of objects created are 4---Ba B1 B2 and Bb

After the execution of the statement B x = m1(new B("Ba")) 2 objects have been created
1. Object Ba--referenced by bc
2. Object B1--referenced by x
After the execution of the statement B y = m1(new B("Bb")); 2 more objects are created
3. Object Bb--referenced by bc.--Note that at this point Ba is not referenced by any variable and is eligible for GC
4. Object B2--referenced by y

Now you have 2 possible outcomes...
If GC does not run before your print Statement...your output will be ,B1,B2,Bb
this option is not given in the answer choices !

If GC runs-- output is Ba,B1,B2,Bb
Of course the Ba part of the output is from the finalize() method
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops !!

looks like i got just a tad bit late in replying !!
I see Kitty has already cleared her doubt !

Dont you hate it whn that happens to you.. :roll:
[ August 17, 2004: Message edited by: Murtuza Akhtari ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic