• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Garbage collection

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the code. Which line of code marks the earliest point that an object referenced by myInt becomes a candidate for garbage collection?
1 public void doStuff() {
2 Integer arr[] = new Integer[5];
3 for (int i = 0; i < arr.length; i++) {
4 Integer myInt = new Integer(i);
5 arr[i] = myInt;
6 }
7 System.out.println("end");
8 }
A) Line 4
B) Line 5
C) Line 6
D) Line 7


what should be the answer ??more important is why??!!
 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harsh, welcome to javaranch.

Harsh when you post a question, it is mandatory to Quote Your Sources. So please tell us where you copied this question from. Also as you can see that myInt is assigned to the array arr, so the objects that myInt points to will be eligible for garbage collection when arr goes out of scope...
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is never available for GC inside the for loop (Because each time a new myInt is created it's reference is assigned to one of the array references). So I guess line 7 where the array is out of scope. Ranchers please correct me if I am wrong.
 
harsh anajwala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry mates...was not aware of rules..
i got this question in one of the mock test i took on following link..
http://scjptest.com/

and how on line7 array is out of scope?
 
harsh anajwala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ankit...and by the way congrats for your score..!!!
More specifically the answer should be Line 8??what say??yeah it's not given in option..but still it should have been given then..
 
Ankit Garg
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to be an error in the mock exam to me...
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes harsh line 8 sorry , my eyes are playing tricks on me I thought the array was declared inside the for loop.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic