aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection and Post Increment Operator doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection and Post Increment Operator doubts" Watch "Garbage Collection and Post Increment Operator doubts" New topic
Author

Garbage Collection and Post Increment Operator doubts

Abhisek Ohdedar
Greenhorn

Joined: Dec 04, 2010
Posts: 2
Hi,

i am taking preparations for SCJP and of course the book written by Sierra-Bates is my guideline .

I was going through mock SCJP questions on the net and came across these.

Question 1.



Here the answer given is "The objects referenced by a and b are eligible for garbage collection."

Can someone please explain the code to me (in details) and explain why object referenced by aa is not eligible for garbage collection?

Question 2.

Given the code. What is the result?

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Abhisek Ohdedar wrote:
Question 2.

Given the code. What is the result?
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?


Take a few seconds, and try it out first...

Henry
Ebenezer Olanrewaju
Greenhorn

Joined: May 31, 2010
Posts: 1
Abhisek Ohdedar wrote:Hi,

i am taking preparations for SCJP and of course the book written by Sierra-Bates is my guideline .

I was going through mock SCJP questions on the net and came across these.

Question 2.

Given the code. What is the result?
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?


It seems I can only provide answer to Question 2.

Ok, let's look at it together:



means the <= condition is tested before the incrementing takes place.

So, the condition is true then i becomes 11
B'cos the condition is true, the inner i++; turns i to 12

Now to the main issue
The system returns to continue with the while loop, tests the condition, discovers it's false, increments i, then leaves the loop to print out the value of i
Thereby making i = 13!



Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


Hi Abhishek,

In first question both A and B objects has cyclic dependency on each other , so if you make one object null and then other also become orphan and eligible for garbage collection , read java spec to understand in detail.

Thanks
Javin.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3216
    
  49

Welcome to JavaRanch, Abhishek.

Whenever you post a question from a mock exam, we require you to QuoteYourSources. Can you please let us know where you got this question from?

And please don't forget to UseCodeTags and UseOneThreadPerQuestion when you post again. I have added the code tags on your post for you.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Frank Callahan
Greenhorn

Joined: Dec 30, 2010
Posts: 15
Hi Abhisek,
When I saw your question (first part) I was puzzled too, and added some code to help me figure it out:



Here's the output I got when I ran it:

Created an A: A@12dacd1 Instance 1
Created a B: B@10385c1 Instance 1
set this.a: A@12dacd1
set this.b: B@10385c1
set aa to: A@12dacd1
Is aa == aa.b.a? true
set aa to null.
Nudged garbage collector.
Finalizing a B B@10385c1
Finalizing an A A@12dacd1

You can see that only 1 instance of A and 1 instance of B were instantiated, so there are only two objects to garbage collect after the aa reference to the instance of A is set to null in the GCTest main() method. When the A() constructor is called, it passes a reference to itself to the B(A a) constructor (this.b = new B(this);) and then the B constructor sets its internal A reference to point to the same object as the aa reference in the GCTest main method. So, before aa is set to null there is 1 A object with two references and 1 B object with 1 reference. From the output from the A and B finalizers, you can see that just 2 objects end up being gc'd.

Happy New Year and Regards,
Frank
 
jQuery in Action, 2nd edition
 
subject: Garbage Collection and Post Increment Operator doubts