18 Questions
What happens when an object is dynamically allocated using the new operator?
The object remains in memory until it is garbage collected
What is the purpose of garbage collection?
To release memory occupied by unused objects
What happens to byte, short, and char values in an expression?
They are promoted to int
What happens when an int value is assigned to a byte variable?
The int value is truncated to fit in the byte variable
What is the result of the expression byte t=100; int p=r * s / t
?
The result is an int value
What happens when a double value is assigned to an int variable?
The double value is truncated to fit in the int variable
What is the purpose of the garbage collector in Java?
To automatically deallocate memory occupied by unused objects
Why is garbage collection necessary in Java?
Because objects occupy memory that can be reclaimed
What happens when an object is no longer referenced in Java?
The object is assumed to be no longer needed and its memory can be reclaimed
What is the purpose of the finalize() method in Java?
To define specific actions to occur when an object is being reclaimed
When does garbage collection occur in Java?
Sporadically during the execution of a program
Why is finalization necessary in Java?
To free non-Java resources such as file handles or character fonts
What is the primary purpose of the finalize( ) method?
To perform actions before an object is destroyed
What is the purpose of the protected specifier in the finalize( ) method?
To prevent access to finalize( ) by code defined outside its class
What is the output of the following code: public class TestGarbage1 { public void finalize() { System.out.println("object is garbage collected"); } public static void main(String args[]) { TestGarbage1 s1=new TestGarbage1(); TestGarbage1 s2=new TestGarbage1(); s1=null; s2=null; System.gc(); } }
object is garbage collected object is garbage collected
What is the purpose of the System.gc() method?
To request garbage collection
What are command-line arguments in Java?
Information passed to the main( ) method as an array of strings
How are command-line arguments stored in a Java program?
As an array of strings
Learn about the difference in memory management between C++ and Java, specifically how Java's garbage collection works to automatically deallocate unused objects.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free