Java Programming Multiple Choice Questions - Garbage Collection
This section focuses on the "Garbage Collection" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. What allows the programmer to destroy an object x?
A. x.finalize()
B. x.delete()
C. Runtime.getRuntime().gc()
D. Only the garbage collection system can destroy an object.
View Answer
Ans : D
Explanation: When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded.
2. Which statement is true?
A. Programs will not run out of memory.
B. Objects that are referred to by other objects will never be garbage collected.
C. Objects that will never again be used are eligible for garbage collection.
D. Objects that can be reached from a live thread will never be garbage collected.
View Answer
Ans : D
Explanation: C See the note above on Islands of Isolation (An object is eligible for garbage collection when no live thread can access it - even though there might be references to it).
B is wrong. ""Never again be used"" does not mean that there are no more references to the object.
A is wrong. Even though Java applications can run out of memory there another answer supplied that is more right.
3. Which of the below is not a memory leak solution?
A. GC parameter tuning
B. Code changes
C. Process restart
D. JVM parameter tuning
View Answer
Ans : C
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will resurge again..
4. Which of the below is not a Java Profiler?
A. JVM
B. JProfiler
C. JConsole
D. Eclipse Profiler
View Answer
Ans : A
Explanation: Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.
5. Which statement is true?
A. All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
B. Objects from a class with the finalize() method overridden will never be garbage collected.
C. Objects with at least one reference will never be garbage collected.
D. Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
View Answer
Ans : D
Explanation: All objects are placed in the garbage collectible heap.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
A. jmap
B. jvmmap
C. memorymap
D. memorypath
View Answer
Ans : A
Explanation: We can use jmap as jmap -J-d64 -heap pid.
7. What is -Xms and -Xmx while starting jvm?
A. Initial; Maximum memory
B. Initial memory
C. Maximum memory
D. Maximum; Initial memory
View Answer
Ans : A
Explanation: JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. java -Xmx2048m -Xms256m.
8. Which of the following has the highest memory requirement?
A. Stack
B. Class
C. JVM
D. Heap
View Answer
Ans : C
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
9. Where is a new object allocated memory?
A. Young space
B. JVM
C. Young or Old space depending on space availability
D. Old space
View Answer
Ans : A
Explanation: A new object is always created in young space. Once young space is full, a special young collection is run where objects which have lived long enough are moved to old space and memory is freed up in young space for new objects..
10. Which of the following is a garbage collection technique?
A. Sweep model
B. Mark and sweep model
C. Space management model
D. Cleanup model
View Answer
Ans : B
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.
11. When is the B object, created in line 3, eligible for garbage collection?
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println(""start completed""); /* Line 7 */
}
A. after line 5
B. after line 6
C. after line 7
D. There is no way to be absolutely certain.
View Answer
Ans : D
Explanation: No explanation.
12. How many objects are eligible for garbage collection after execution of line ?
public class Test
{
public static void main(String[] args)
{
m1(); // Line
}
static void m1()
{
Test t1 = new Test();
Test t2 = new Test();
}
}
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: Since t1 and t2 are local objects of m1() method, so they become eligible for garbage collection after complete execution of method unless any of them is returned.
13. After line 11 runs, how many objects are eligible for garbage collection?
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them..
14. When is the Demo object eligible for garbage collection?
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
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.
View Answer
Ans : D
Explanation: D is correct. By a process of elimination.
15. How many objects are eligible for garbage collection after execution of line 8?
public class Test
{
public static void main(String [] args)
{
Test t1 = new Test();
Test t2 = m1(t1); // line 6
Test t3 = new Test();
t2 = t3; // line 8
}
static Test m1(Test temp)
{
temp = new Test();
return temp;
}
}
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : B
Explanation: By the time line 8 has executed, the only object without a reference is the one generated i.e as a result of line 6. Remember that ""Java is strictly pass by value"" so the reference variable t1 is not affected by the m1() method. We can check it using finalize() method. The statement ""System.out.println(this.hashcode())"" in finalize() method print the object hashcode value on which finalize() method is called,and then just compare the value with other objects hashcode values created in main m
16. When is the Float object, created in line 3, eligible for garbage collection?
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
A. just after line 5
B. just after line 6
C. just after line 7
D. just after line 8
View Answer
Ans : C
Explanation: No explnation.
Also check :
Discussion