Slowly getting there.. please ask questions
1. Which three of the following statements are true?
1 finalize() is called only once for each object instance
2 The garbage collector frees memory in the heap
3 You can force the garbage collector to operate
4 An object only used by a blocked
thread can be cleaned up
5 An object with a reference to it will never be cleaned up by the garbage
collector
6 finalize() may never run
7 A
java application will not run out of memory because of the garbage
collector
2. What is the output of the following code?
1 Compile error
2 Run time exception thrown
3 memoryemptyclean
4 memoryclean
5 memory empty
6 The output can not be known for certain beyond that memory will be output
before empty, with clean possibly printed between them or after empty.
3 The correct syntax for requesting garbage collection is:
1 class garbage extends garbageCollector { public void collect(); }
2 system.
3 GarbageCollection();
4 system.deleteUnusedObjects();
5 system.GarbageCollection();
6 system.gc();
7 None of the above
4 Which line of code inserted //Here will free up a single object for garbage
collection?
1 a.i = null;
2 b.i = a.i;
3 b = null;
4 a = d.i;
5 d.i = b.i;
6 e = null;
5 At the line // Here, how many objects are eligible for garbage collection?
1 Compile error
2 Run time exception
3 0 objects available for collection
4 1 object available for collection
5 2 objects available for collection
6 3 objects available for collection
6 At the line // Here, how many objects are eligible for garbage collection?
1 Compile error
2 Run time exception
3 0 objects available for collection
4 1 object available for collection
5 2 objects available for collection
6 3 objects available for collection
1 Objective 3.1
1,2,6 are true
1 - True. finalize() is called once for each object instance
2- True. The garbage collector operates on the heap
3 - False. You can request the garbage collector be run, but it may not be.
4 - False. An object used by a blocked thread can not be cleaned up.
5 - False. An object with a reference to it may be referenced by an object
which itself is not referenced, and both can be cleaned up.
6 - True. Finalize may never run
7 - False. A Java application can still run out of memory
2 Objective 3.1
Answer - 1
The method finalize is void protected finalize, so it can not be compiled. The garbage collector does not guarantee when or if it will call finalize, so one can not be sure what the output is. It may or may not print "clean" at any point. (In my tests, it never does, but that is implementation dependent on the JVM)
3 Objective 3.2
Answer 7 - none of the above. System.gc() is the correct syntax (capitalized System)
4 Objective 3.2
Answer 5 d.i = b.i
At the line // Here the only object which has a single reference to it
(available to be erased) is the object formerly pointed to by a. This is where d.i points. So changing d.i causes nothing to refer to it, so it can be cleaned up.
5 Objective 3.3
Answer 3- 0 objects available for collection. At // Here, every object still has a reference to it.
6 Objective 3.3
Answer - 5 two objects available for collection. The object originally pointed to by a and the object originally pointed to by b are pointing to each other but no other reference exists to them.
Section 3: Garbage Collection
1 State the behavior that is guaranteed by the garbage collection system.
2 Write code that explicitly makes objects eligible for garbage collection.
3 Recognize the point in a piece of source code at which an object becomes
eligible for garbage collection.