Object-Oriented Programming: Garbage Collection
18 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What happens when an object is dynamically allocated using the new operator?

  • The object is immediately destroyed
  • The object is automatically deallocated after use
  • The object is converted to a primitive data type
  • The object remains in memory until it is garbage collected (correct)
  • What is the purpose of garbage collection?

  • To convert objects to primitive data types
  • To release memory occupied by unused objects (correct)
  • To promote byte values to int
  • To allocate memory for new objects
  • What happens to byte, short, and char values in an expression?

  • They are promoted to int (correct)
  • They are promoted to long
  • They are promoted to double
  • They are promoted to float
  • What happens when an int value is assigned to a byte variable?

    <p>The int value is truncated to fit in the byte variable</p> Signup and view all the answers

    What is the result of the expression byte t=100; int p=r * s / t?

    <p>The result is an int value</p> Signup and view all the answers

    What happens when a double value is assigned to an int variable?

    <p>The double value is truncated to fit in the int variable</p> Signup and view all the answers

    What is the purpose of the garbage collector in Java?

    <p>To automatically deallocate memory occupied by unused objects</p> Signup and view all the answers

    Why is garbage collection necessary in Java?

    <p>Because objects occupy memory that can be reclaimed</p> Signup and view all the answers

    What happens when an object is no longer referenced in Java?

    <p>The object is assumed to be no longer needed and its memory can be reclaimed</p> Signup and view all the answers

    What is the purpose of the finalize() method in Java?

    <p>To define specific actions to occur when an object is being reclaimed</p> Signup and view all the answers

    When does garbage collection occur in Java?

    <p>Sporadically during the execution of a program</p> Signup and view all the answers

    Why is finalization necessary in Java?

    <p>To free non-Java resources such as file handles or character fonts</p> Signup and view all the answers

    What is the primary purpose of the finalize( ) method?

    <p>To perform actions before an object is destroyed</p> Signup and view all the answers

    What is the purpose of the protected specifier in the finalize( ) method?

    <p>To prevent access to finalize( ) by code defined outside its class</p> Signup and view all the answers

    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(); } }

    <p>object is garbage collected object is garbage collected</p> Signup and view all the answers

    What is the purpose of the System.gc() method?

    <p>To request garbage collection</p> Signup and view all the answers

    What are command-line arguments in Java?

    <p>Information passed to the main( ) method as an array of strings</p> Signup and view all the answers

    How are command-line arguments stored in a Java program?

    <p>As an array of strings</p> Signup and view all the answers

    More Like This

    Use Quizgecko on...
    Browser
    Browser