aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Que regarding Dan's Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Que regarding Dan Watch "Que regarding Dan New topic
Author

Que regarding Dan's Exam

Kitty Dayal
Ranch Hand

Joined: Jul 22, 2004
Posts: 89
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,
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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 ]

"Eppur si muove!"
Kitty Dayal
Ranch Hand

Joined: Jul 22, 2004
Posts: 89
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
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
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


---<br />SCJP 1.4
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Que regarding Dan's Exam