In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage or memory occupied by objects that are no longer in use by the program.
2. 1. What does garbage collection do?
a. Memory Management technique.
b. Process of freeing objects.
c. No longer referenced by the program.
d. All of above
2. In .Net, which pointer does Managed heap use to indicate the position where next object
is to be allocated within the heap?
a. Next
b. Ptr
c. NextObjPtr
d. Pointer is not used
3. Ways for making objects eligible for collection that are no longer needed
a. Nulling a reference
b. Reassigning a reference variable
c. Isolating a reference
d. All of above
4. Which part of the memory is involved in Garbage Collection?
a. Stack
b. Heap
c. Both
5. How many times does the garbage collector calls the finalize() method for an object?
a. Once
b. Never
c. Twice
6. What happens if an uncaught exception is thrown from during the execution of the
finalize() method of an object?
a. The exception will be ignored and the garbage collection (finalization) of that
object terminates.
b. Program crashes due to that exception.
c. Depends on the type of exception
d. None of above
7. How to call garbage collector?
3. a. System.gc()
b. Runtime.getRuntime().gc()
c. Both a & b
d. None of them
8. Why is it good?
a. Manual memory management is time consuming, and error prone
b. Most programs still contain leaks when using manual memory management
c. Both a & b
d. None of above
9. Is garbage collector a dameon thread?
a. Yes
b. No
c. Not sure
10. What does allows user to destroy an object x in Java?
a. x.delete()
b. x.finalize()
c. Runtime.getRuntime().gc()
d. Only the garbage collection system can destroy an object.
11. Which operator is used by Java run time implementations to free the memory of an object
when it is no longer needed?
a. Delete
b. Free
c. New
d. None of the mentioned
12. Which function is used to perform some action when the object is to be destroyed?
a. Finalize()
4. b. delete()
c. main()
d. None of the mentioned
13. Which of the following statements are incorrect?
a. Default constructor is called at the time of declaration of the object if a
constructor has not been defined.
b. Constructor can be parameterized.
c. finalize() method is called when a object goes out of scope and is no longer
needed.
d. finalize() method must be declared protected.
14.
15. void start() {
16. A a = new A();
17. B b = new B();
18. a.s(b);
19. b = null; /* Line 5 */
20. a = null; /* Line 6 */
21. System.out.println("start completed"); /* Line 7 */
22. }
When is the B object, created in line 3, eligible for garbage collection?
a. After line 5
b. After line 6
c. After line 7
d. There is no certain way of knowing
15.
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
5. } /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
When is the Demo object eligible for garbage collection?
a. After line 7
b. After line 8
c. After the start() method completes
d. When the instance running this code is made eligible for garbage collection.
16.
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
a. 0
b. 1
c. 2
d. 3
17.
6. 18. public Object m()
19. {
20. Object o = new Float(3.14F);
21. Object [] oa = new Object[l];
22. oa[0] = o; /* Line 5 */
23. o = null; /* Line 6 */
24. oa[0] = null; /* Line 7 */
25. return o; /* Line 8 */
26. }
When is the Float object, created in line 3, eligible for garbage collection?
a. After line 5
b. After line 6
c. After line 7
d. After line 8
18.
19. class X2
20. {
21. public X2 x;
22. public static void main(String [] args)
23. {
24. X2 x2 = new X2(); /* Line 6 */
25. X2 x3 = new X2(); /* Line 7 */
26. x2.x = x3;
27. x3.x = x2;
28. x2 = new X2();
29. x3 = x2; /* Line 11 */
30. doComplexStuff();
31. }
32. }
after line 11 runs, how many objects are eligible for garbage collection?
a. 0
b. 1
c. 2
d. 3
19.
7. 20. class Bar { }
21. class Test
22. {
23. Bar doBar()
24. {
25. Bar b = new Bar(); /* Line 6 */
26. return b; /* Line 7 */
27. }
28. public static void main (String args[])
29. {
30. Test t = new Test(); /* Line 11 */
31. Bar newBar = t.doBar(); /* Line 12 */
32. System.out.println("newBar");
33. newBar = new Bar(); /* Line 14 */
34. System.out.println("finishing"); /* Line 15 */
35. }
36. }
At what point is the Bar object, created on line 6, eligible for garbage collection?
a. after line 12
b. after line 14
c. after line 7, when doBar() completes
d. after line 15, when main() completes
20.
21. class HappyGarbage01
22. {
23. public static void main(String args[])
24. {
25. HappyGarbage01 h = new HappyGarbage01();
26. h.methodA(); /* Line 6 */
27. }
28. Object methodA()
29. {
30. Object obj1 = new Object();
31. Object [] obj2 = new Object[1];
32. obj2[0] = obj1;
33. obj1 = null;
34. return obj2[0];
35. }
36. }
Where will be the most chance of the garbage collector being invoked?
8. a. After line 9
b. After line 10
c. After line 11
d. Garbage collector never invoked in methodA()
21. Which statement is true?
a. Programs will not run out of memory
b. Objects that will never again be used are eligible for garbage collection.
c. Objects that are referred to by other objects will never be garbage collected.
d. Objects that can be reached from a live thread will never be garbage collected.
22. Which statement is true?
a. All objects that are eligible for garbage collection will be garbage collected by the
collector.
b. Objects with at least one reference will never be garbage collected.
c. Objects from a class with the finalize() method overridden will never be garbage
collected.
d. Objects instantiated within anonymous inner classes are placed in the garbage
collectible heap.
23. Which of the following is not an approach for garbage collection?
a. Copying
b. Tracing
c. Reference Counting
d. The plane algorithm
24. What does reference counting do?
a. Count for each object.
b. Count increases as number of reference increases.
c. Eligible for GC when count equals zero.
d. All of above
25. Tracing is also known as
a. Mark algorithm
b. Mark and Sweep algorithm
9. c. Sweeping algorithm
d. None of them
26. Which objects are not reachable in tracing?
a. Marked ones
b. Unmarked ones
c. None of above
27. Which approach works on the basis of lifetimes of objects.
a. Generational Collectors
b. Tracing
c. Reference counting
d. Copying
28. Which of the following statement is incorrect?
a. Finalize is very different from destructors.
b. Finalize is same as destructors.
c. Finalizable objects may refer to other (non-finalizable) objects, prolonging their
lifetime unnecessarily.
d. None of them
29. What is resurrection?
a. An object requiring finalization dies, lives, and then dies again, this phenomenon
is called resurrection.
b. An object requiring finalization lives, and then dies again, this phenomenon is
called resurrection.
30. Which objects are collected before others in generational collectors?
a. Short lived objects
b. Long lived objects
c. Any object can be collected at any time
d. None of above
Video Link: https://www.youtube.com/watch?v=7AcptSGscKY&feature=youtu.be