• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Dan's test question on Garbage collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all:

I am wondering the answer to this question on dan's test:
void m1() {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(); // 1
m2(q1); // 2
}
System.out.print("All done"); // 3
}

When the processing of line 3 begins, how many objects of type Q that were created at line 1 have become eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compile-time error
g. Run-time error
h. None of the above

---
Answer according to Dan's site is [e]. Having said that if you observe the question carefully & go nuts about the method 'm2(q1)' and wondering where in the world is the 'meat' for m2(), that made me select the option [F], 'F' like in flower...and after reading the answer, I am not sure if my thinking was normal or abnormal.

Any suggestions are appreciated. In the morning I felt greatly motivated to prepare for SCJP. Having read the smallest topic (Garbage C), and after taking the test at dan's, I feel a bit confused ??

--thanks
VJ
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jay vmal:
if you observe the question carefully & go nuts about the method 'm2(q1)' and wondering where in the world is the 'meat' for m2(), that made me select the option [F], 'F' like in flower...and after reading the answer, I am not sure if my thinking was normal or abnormal.



m2() is not written. Its code isn't shown.

Ah, the case of the old create an object and pass the object to a method that's not shown in the code. Who knows what that unwritten method could do to that object, it might *gulp* assign the object to another variable.
Or worse yet, it might still be alive after the current class is garbage collected.

Imagine this


Here an object s1 is created in main(). The object s1 is passed to m2, and since it gets assigned to a variable, it doesn't get garbage collected after main() ends.
 
jay vmal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Louie van Bommel:
Thanks for taking time to reply. What I was expecting was your answer for that question. As there is no meat for m2()...if you compile that code it should give a compilation error ? am I right....

-VJ
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like your real question here is:

If I am given a chunk of code, and it seems to not be a complete program, should I assume there is another part of the program I can't see?

And the answer is:

On Dan's test, yes there is another part of the program you can not see.

On the real test, I don't know if you should assume other sections of code do or do not exist.
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jay vmal:
As there is no meat for m2()...if you compile that code it should give a compilation error ? am I right....

-VJ



Jay has a valid point. I'll wait for Dan to reply
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic