Computer Science, asked by fatimakhalid070782, 4 months ago

What is the output of 11/10?
Give Correct Answer I will Mark You As Brainlist​

Answers

Answered by Shwetatheprincess
1

Output of Java programs | Set 10 (Garbage Collection)

Prerequisite – Garbage Collection in Java

Difficulty level : Intermediate

In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection.

Predict the output of following Java Programs:

Program 1 :

public class Test

{

public static void main(String[] args) throws InterruptedException

{

String str = new String("GeeksForGeeks");

// making str eligible for gc

str = null;

// calling garbage collector

System.gc();

// waiting for gc to complete

Thread.sleep(1000);

System.out.println("end of main");

}

@Override

protected void finalize()

{

System.out.println("finalize method called");

}

}

Output:

end of main

Explanation : We know that finalize() method is called by Garbage Collector on an object before destroying it. But here, the trick is that the str is String class object, not the Test class. Therefore, finalize() method of String class(if overridden in String class) is called on str. If a class doesn’t override finalize method, then by default Object class finalize() method is called.

Program 2 :

public class Test

{

public static void main(String[] args) throws InterruptedException

{

Test t = new Test();

// making t eligible for garbage collection

t = null;

// calling garbage collector

System.gc();

// waiting for gc to complete

Thread.sleep(1000);

System.out.println("end main");

}

@Override

protected void finalize()

{

System.out.println("finalize method called");

System.out.println(10/0);

}

}

Output:

finalize method called

end main

Explanation :

When Garbage Collector calls finalize() method on an object, it ignores all the exceptions raised in the method and program will terminate normally.

Program 3 :

public class Test

{

static Test t ;

static int count =0;

public static void main(String[] args) throws InterruptedException

{

Test t1 = new Test();

// making t1 eligible for garbage collection

t1 = null; // line 12

// calling garbage collector

System.gc(); // line 15

// waiting for gc to complete

Thread.sleep(1000);

// making t eligible for garbage collection,

t = null; // line 21

// calling garbage collector

System.gc(); // line 24

// waiting for gc to complete

Thread.sleep(1000);

System.out.println("finalize method called "+count+" times");

}

@Override

protected void finalize()

{

count++;

t = this; // line 38

}

}

Output:

finalize method called 1 times

Explanation :

After execution of line 12, t1 becomes eligible for garbage collection. So when we call garbage collector at line 15, Garbage Collector will call finalize() method on t1 before destroying it. But in finalize method, in line 38, we are again referencing the same object by t, so after execution of line 38,this object is no longer eligible for garbage collection. Hence, Garbage Collector will not destroy the object.

Now again in line 21, we are making same object eligible for garbage collection one more time. Here, we have to clear about one fact about Garbage Collector i.e. it will call finalize() method on a particular object exactly one time. Since on this object, finalize() method is already called, so now Garbage Collector will destroy it without calling finalize() method again.

Program 4 :

public class Test

{

public static void main(String[] args)

{

// How many objects are eligible for

// garbage collection after this line?

m1(); // Line 5

}

static void m1()

{

Test t1 = new Test();

Test t2 = new Test();

}

}

Question :

How many objects are eligible for garbage

Please mark me as brainliest...

Similar questions