Java Memory Management MCQs

11.) What is the purpose of the “finalize()” method in Java?

A) To explicitly deallocate memory for an object.
B) To reclaim memory for unreachable objects.
C) To prevent objects from being garbage collected.
D) To optimize CPU usage.

Answer: Option B

Explanation: The “finalize()” method is used to give an object an opportunity to clean up resources before it is reclaimed by the Garbage Collector.

12.) Which Java memory area stores method-specific data, including local variables and parameters?

A) Stack memory
B) Heap memory
C) PermGen (Metaspace in Java 8+) memory
D) Garbage collection memory

Answer: Option A

Explanation: Method-specific data, including local variables and parameters, is stored in the Stack memory.

13.) What is the default memory area for storing string literals in Java?

A) Stack memory
B) Heap memory
C) PermGen (Metaspace in Java 8+) memory
D) Garbage collection memory

Answer: Option B

Explanation: String literals are typically stored in the Heap memory.

14.) What is a memory leak in Java?

A) When an object holds a reference to another object.
B) When an object consumes more memory than it needs.
C) When an object is unintentionally retained in memory and cannot be garbage collected.
D) When an object is not initialized properly.

Answer: Option C

Explanation: A memory leak in Java occurs when an object is unintentionally retained in memory and cannot be garbage collected, leading to excessive memory consumption.

15.) When is the “finalize()” method called on an object by the Garbage Collector?

A) Immediately after the object is created.
B) When the object is explicitly requested to be finalized by the programmer.
C) Just before the object is garbage collected.
D) When the object is marked as unreachable.

Answer: Option C

Explanation: The “finalize()” method is called on an object just before the object is garbage collected.

16.) Which Java memory area is responsible for storing thread-specific data, such as thread-local variables?

A) Stack memory
B) Heap memory
C) PermGen (Metaspace in Java 8+) memory
D) Thread-local memory

Answer: Option D

Explanation: Thread-specific data, such as thread-local variables, is stored in thread-local memory.

17.) What is the primary purpose of the “System.gc()” method in Java?

A) To explicitly deallocate memory for a specific object.
B) To request the Garbage Collector to run.
C) To optimize CPU usage.
D) To mark an object as unreachable.

Answer: Option B

Explanation: The “System.gc()” method is used to request the Garbage Collector to run, although it does not guarantee immediate garbage collection.

18.) Which memory area is responsible for storing objects with a longer lifespan, such as application-wide caches?

A) Stack memory
B) Heap memory
C) PermGen (Metaspace in Java 8+) memory
D) Tenured memory

Answer: Option D

Explanation: Objects with a longer lifespan, such as application-wide caches, are typically stored in Tenured memory within the Heap.

19.) What is the primary purpose of the Young Generation in Java’s Garbage Collection process?

A) To store long-lived objects.
B) To store short-lived objects.
C) To store method code and class metadata.
D) To store thread-specific data.

Answer: Option B

Explanation: The primary purpose of the Young Generation is to store short-lived objects that are likely to be garbage collected quickly.

20.) What is the purpose of the “Eden” space in the Java Heap?

A) To store long-lived objects.
B) To store short-lived objects.
C) To store class metadata.
D) To store native method code.

Answer: Option B

Explanation: The “Eden” space in the Java Heap is used to store short-lived objects that are likely to be garbage collected quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *