(Quiz) SCJP (Sun Certified Java Program) Garbage Collection and Object Lifetime
QUIZ: SCJP (Sun Certified Java Program) Garbage Collection and Object Lifetime
Questions 1 Which of the following statements about Java grabage collection are true statements?
(a) The following code will start the garabage collection mechanism:
Runtime.getRuntime().gc();
Thread.yield();
(b) The following code will start the garbage collection mechanism:
System.gc();
Thread.sleep(1000);
(c) The garbage collection Thread has a low priority.
(d) The method by which Java tracks unused objects is specified in the language definition.
(e) The method by which Java determines that a chunk of memory is garbage is up to the implementor of the
JVM.
Answers: 1 (c), (e)
Answer (c) is correct because the JVM ssigns a low priority to the garbage collection thread. Answer (e) is also correct because picking the best garbage collection method is left up to the individual JVM
implementor. Answer (a), (b) are incorrect because of the phrase “will start” these code fragments “may” start the garbage collector, but there is no guarantee. Answer (d) is incorrect because the langauge specification does not prescribe a method.
Questions 2 Which of the following statements about finalize methods is true?
(a) The purpose of a finalize method is to recover system resources other than memory.
(b) The purpose of a finalize method is to recover memory and other system resources.
(c) You should always write a finalize method for every class.
(d) The order in which objects are created controls the order in which their finalize methods are called.
Answers: 2 (a)
Answer (b) is incorrect because memory is recovered by garbage collection; finalizers are for other resources. Answer (c) is incorrect because objects that do not use system resources other than memory do not need
finalizers. Answer (d) is incorrect because there is no guarantee about the order of object finalization.
Questions 3 After which line the object initially refered by str is eligible for garbage
collection?
Select one correct answer.
1 class Garbage
2 {
3 public static void main(String arg[])
4 {
5 String str=new String("Hello");
6 String str1=str;
7 str=new String("Hi");
8 str1=new String("Hello Again");
9 return;
10 }
11 }
(a) After line no. 6
(b) After line no. 7
(c) After line no. 8
(d) After line no. 9
Answers:3 (c)
At line no. 7 str refers to a new object but its previous object is still refered by str1 at that time.At line no. 8 str1 changes its reference then the object initially refered by str is eligible for garbage collection.
- guru's blog
- Login to post comments
