About Question com.enthuware.ets.scjp.v6.1.796 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.1.796 :

Post by ETS User »

When I run the code below, I get these two answers.
It may print hello, hello, and world in any order.(I get this most of the time)
One possible output is: hello world(I get this sometimes)

The correct answer in the question is,
One possible output is: hello (I have never managed to get this output at all)
One possible output is: hello world

public class FinalizeTest {
private String value;
public FinalizeTest(String text) {
this.value = text;
}
public static void main(String[] args) throws Throwable{
FinalizeTest f1 = new FinalizeTest("hello");
FinalizeTest f2 = new FinalizeTest("world");
f1.finalize();
f1 = null; f2 = null;
System.gc();
}
public void finalize() throws Throwable{
System.out.println(this.value);
super.finalize();
}
}

Thank You.

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.1.796 :

Post by admin »

It is not necessary that all the outputs will be reproducible in all situations. A particular behavior may occur only in some situations. In this case, as the explanation notes, it is possible that System.gc() may not actually invoke the GC thread and that thread may not call the finalize() method of the objects at all. So in this case, only hello will be printed.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question com.enthuware.ets.scjp.v6.1.796 :

Post by Guest »

Then all three options is theoretically possible, including the one below witch was not a part of the correct answer, sense GC might call finalize() even do you did it manually, correct?

It may print hello, hello, and world in any order.(I get this most of the time)

Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests