Review Questions



Which statement is true?

Select the one correct answer.

  1. Objects can be explicitly destroyed using the keyword delete.

  2. An object will be garbage collected immediately after it becomes unreachable.

  3. If object obj1 is accessible from object obj2, and object obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection.

  4. Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed.

  5. If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.


Identify the location in the following program where the object, initially referenced with arg1, is eligible for garbage collection.

public class MyClass {
    public static void main(String[] args) {
        String msg;
        String pre = "This program was called with ";
        String post = " as first argument.";

        String arg1 = new String((args.length > 0) ?
                           "'" + args[ 0 ] + "'" :
                           "<no argument>");
        msg = arg1;
        arg1 = null;             // (1)
        msg = pre + msg + post;  // (2)
        pre = null;              // (3)


        msg = null;              // (4)
        post = null;             // (5)
        args = null;             // (6)

Select the one correct answer.

  1. After the line labeled (1).

  2. After the line labeled (2).

  3. After the line labeled (3).

  4. After the line labeled (4).

  5. After the line labeled (5).

  6. After the line labeled (6).


Which statement is true?

Select the one correct answer.

  1. If an exception is thrown during the execution of the finalize() method of an eligible object, then the exception is ignored and the object is destroyed.

  2. All objects have a finalize() method.

  3. Objects can be destroyed by explicitly calling the finalize() method.

  4. The finalize() method can be declared with any accessibility.

  5. The compiler will fail to compile code that defines an overriding finalize() method that does not explicitly call the overridden finalize() method from the superclass.


Which statement is true?

Select the one correct answer.

  1. The compiler will fail to compile code that explicitly tries to call the finalize() method.

  2. The finalize() method must be declared with protected accessibility.

  3. An overriding finalize() method in any class can always throw checked exceptions.

  4. The finalize() method can be overloaded.

  5. The body of the finalize() method can only access other objects that are eligible for garbage collection.


Which statement describes guaranteed behavior of the garbage collection and finalization mechanisms?

Select the one correct answer.

  1. Objects will not be destroyed until they have no references to them.

  2. The finalize() method will never be called more than once on an object.

  3. An object eligible for garbage collection will eventually be destroyed by the garbage collector.

  4. If object A became eligible for garbage collection before object B, then object A will be destroyed before object B.

  5. An object, once eligible for garbage collection, can never become accessible by a live thread.