© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

Home Placements Interview Questions Java Interview Questions And Answers Java Interview Questions On Garbage Collection ▼

Java Interview Questions On Garbage Collection

1. What is mean by garbage collection in java?

Answer

One of the most important features of the Java is garbage collection, also called automatic memory management. In Java while the program is running, the memory is being allocated with new operation. This allocated memory storage is not available until garbage collector sweeps away the unused objects. To make any object unusable make the reference variable pointing to that object as null pointer. In Java all the objects are garbage collected, when you make a null reference to the object. In Java you never explicitly free the memory allocated, instead the Java does automatic garbage collection.

2. Explain About finalize method?

Answer
The finalize() method will be called before the garbage collector sweeps away the object. In practice, we do not rely on the finalize() method for recycling any resources that are in short supply - you simply cannot know when this method will be called.

3. Does garbage collection guarantee that a program will not run out of memory?

Answer
Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection

4. How can you force garbage collection?
[a] Garbage collection cannot be forced
[b] Call System.gc().
[c] Call Runtime.gc().
[d] Set all references to null.

Answer
[a] Garbage collection cannot be forced

5. Which statements about garbage collection are true?
[a] The garbage collector runs in low memory situations
[b] You can run the garbage collector when ever you want.
[c] When it runs it releases the memory allocated by an object.
[d] Garbage collector immediately runs when you set the references to null.

Answer
[a] The garbage collector runs in low memory situations
[c] When it runs it releases the memory allocated by an object

6. From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
[a] 1
[b] 2
[c] 3
[d] 0

Answer
[a] 1

SLogix Student Projects
bottom