aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please solve the following Problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please solve the following Problem." Watch "Please solve the following Problem." New topic

Please solve the following Problem.

Shishir malviya

Joined: Jun 21, 2005
Posts: 25
How many objects will be eligible for garbage collection after line 7?


A. 0
B. 1
C. 2
D. 3
E. Code does not compile
[ July 02, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please tell us what you think the answer is.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I guess the answer is 3.

SCJP 1.4, SCWCD 1.4, SCBCD 1.5
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Only 1 object(new Integer(100)) eligible for Garbage collection

Other 2 have references to it
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Only 1 object(new Integer(100)) eligible for Garbage collection
Other 2 have references to it

Should it not be 2 objects ,
1. IntegerObject - a
2. The String used in the constructor - "100" ( ??? )
[ July 02, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
i agreed with vidya.

about the member who posted above me, i can't see the name right now replying but the member said about shouldn't it be 2 objects be elibgable for garbage collection.

Integer Object will be the one who eligable for GC.

String Object wont be elibgable because it was not set to null.

c=b; // here said c refers to b which c is new variable given to new String.

so the c in the "String c= new String("100"); still not yet elibgable.

correct me if i'm wrong. thanks...

and this question i saw it b4. i bet most of members here do too.

all thebest
[ July 02, 2005: Message edited by: Nicky Eng ]

From NickyEng
Diploma in Computer Studies
SCJP 1.4
Formula 1 app by Maxis (Playbook)
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
I aggry with u Vidya.

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


A. 0
B. 1
C. 2
D. 3
E. Code does not compile

[ July 02, 2005: Message edited by: Barry Gaunt ][/qb]<hr></blockquote>

The answer is 1.
Three objects are created during the execution of the main method and one object is created during class (TutorialGC) load time representing the String literal. At line 7 (after method execution), one of the objects thas has been created (with the new keyword) have no strong references referring to it (note that 'out of scope' does not imply 'not referring to' as is often misunderstood). The other two created objects maintain strong references to them. The object representing the String literal becomes eligible for GC when the object referred to by TutorialGC.class.getClassLoader() becomes eligible (which clearly is not the case on line 7).
[ July 04, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
HaoZhe Xu
Ranch Hand

Joined: Nov 03, 2003
Posts: 222
I think the answer is 1, here's my thinking:
Line 1 - 3
a -> Integer
b -> Long
c -> String

Line 4
a = null
(notice here the previous object that a pointed has no reference to it, so Integer(100) is eligible for GC)

Line 5
a = c
reference a points to the object that c is pointing to (ie. String("100"))

Line 6
c = b
reference c points to the object that b is pointing to (ie. Long(100))
notice here the previous c object (String("100")) still has a reference a

Line 7
b = a
reference b points to the object that a is pointing to (ie. String("100"))
previous b (Long(100)) does not lose reference because in Line 6 c pointed to it

so, finally, only the Integer(100) at the very beginning is eligible for GC

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Please solve the following Problem.
Similar Threads
eligible for garbage collection
output clarification
Objects eligible for garbage collection.
Sun Guoqiao's mock exam about identifiers?