|
1. How many objects are created and how many are eligible for garbage collection just after line 11 is executed?
21. import java.util.Date;
22. public class Tester {
23. public void garbageTest() {
24. Date today = new Date();
25. String s1 = “Created a date object”;
26. Date now = new Date();
27. today = now;
28. }
29. public static void main(String args[]) {
30. Tester tester = new Tester();
31. tester.garbageTest();
32. System.out.println(“done”);
33. }
34. }
A. 5 objects are created and 4 will be eligible for garbage collection.
B. 4 objects are created and 3 will be eligible for garbage collection.
C. 4 objects are created and 4 will be eligible for garbage collection.
D. 1 object is created and 1 will be eligible for garbage collection.
E. 5 object is created and will be eligible for garbage collection.
2. Which of the following statements is true?
A. Garbage collector runs periodically to make sure that sufficient memory is always available.
B. All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
C. Objects with at least one reference will never be garbage collected.
D. Objects of a class with overridden finalize() method will always be garbage collected.
E. None of the above
3. Which of the following statements is true?
A. Simple garbage collector uses reference counting algorithm.
B. You will get as much memory as possible after making a call to System.gc();
C. You can make object unreachable by making all its references null.
D. Object becomes eligible for garbage collection if it stores a reference to another object, object2, and if object2 becomes unreachable (eligible for garbage collection)
4. Consider the following code. How many objects are eligible for garbage collection after the line 9 is executed?
1. public class Test {
2. public Test t;
3. public static void main(String[] args) {
4. Test t1 = new Test();
5. Test t2 = new Test();
6. t1.t = t2;
7. t2.t = t1;
8. t2 = new Test();
9. t1 = t2;
10. }
11. }
A. It is platform dependent.
B. 0
C. 1
D. 2
E. 3
F. 4
G. 6
5. Consider the following code. What should come at line 8 that will make the object created on line 5 eligible for garbage collection?
1. public class Test {
2. public static void main(String [] args) {
3. Test t1 = new Test();
4. Test t2 = t1;
5. t3 = new Test();
6. t4 = t3;
7. t3 = t1;
8. // ?????
9. }
10. }
A. t3 = null;
B. t1 = null;
C. t3 = t1;
D. t4 = null;
E. t4 = t2;
F. Nothing. The object is already eligible for garbage collection.
6. Consider the following code. When at earliest the object created at line 4 will be eligible for garbage collection?
1. public class Car {
2. private Car testDrive() {
3. // do some rough driving…
4. Car newOne = new Car();
5. return newOne;
6. }
7. public static void main(String[] args) {
8. Car mycar = new Car();
9. mycar = mycar.testDrive();
10. Car oldOne = new Car();
11. mycar = oldone;
12. oldone = null;
13. }
14. }
A. After line 5 is executed.
B. After line 9 is executed.
C. After line 10 is executed
D. After line 11 is executed.
E. After line 12 is executed.
F. Only after this program completes its execution.
7. Consider the following code. When at earliest the message “Doing finalization” is printed?
1. public class Car {
2. public static void main(String[] args) {
3. Car c1 = new Car();
4. Car c2 = new Car();
5. c1 = c2;
6. c2 = null;
7. }
8. protected void finalize() throws Throwable{
9. System.out.println(“Doing finalization”);
10. }
11. }
A. After line 4 is executed.
B. After line 5 is executed.
C. After line 6 is executed.
D. Never in the given code. It will be printed only if you call the c1.finalize() or c2.finalize().
E. It cannot be predicted.
8. Consider the following code. How many objects are eligible for garbage collection after method testDrive() is executed.?
1. public class Car {
2. private void testDrive() {
3. int year = 1994;
4. String model = new String(year);
5. String yearOfPurchase = “1994”;
6. String anotherModel = new String(“1994”);
7. }
8. public static void main(String[] args) {
9. Car mycar = new Car();
10. mycar.testDrive();
11. }
12. }
A. 1
B. 2
C. 3
D. 4
E. 0
9. Consider the following code. How many times the message “Garbage collecting the Car object. . .” is printed after executing the following code?
1. public class Car {
2. protected void finalize() throws Throwable {
3. System.out.println("Garbage collecting the Car object. . . ");
4. }
5.
6. public static void main(String[] args) {
7. for (int i = 0; i < 100; i++) {
8. new Car();
9. }
10.
11. }
12. }
A. 100
B. 0
C. 25
D. Never in the given code. The message will be printed 100 times only if you call the System.gc() on line 10.
E. It cannot be predicted.