Java OCA OCP Practice Question 1968

Question

Identify the location in the following program where the object,

initially referenced with arg1, is eligible for garbage collection.

public class Main {
  public static void main(String[] args) {
    String msg;/*from   w ww.  j a  v a2  s.  co  m*/
    String pre = "test ";
    String post = " test.";
    String arg1 = new String((args.length > 0) ? "'" + args[0] + "'" :
                               "<no argument>");
    msg = arg1;
    arg1 = null;              // (1)
    msg = pre + msg + post;   // (2)
    pre = null;               // (3)
    System.out.println(msg);
    msg = null;               // (4)
    post = null;              // (5)
    args = null;              // (6)
  }
}

Select the one correct answer.

  • (a) After the line labeled (1).
  • (b) After the line labeled (2).
  • (c) After the line labeled (3).
  • (d) After the line labeled (4).
  • (e) After the line labeled (5).
  • (f) After the line labeled (6).


(b)

Note

Before (1), the String object initially referenced by arg1 is denoted by both msg and arg1.

After (1), the String object is only denoted by msg.

At (2), reference msg is assigned a new reference value.

This reference value denotes a new String object created by concatenating contents of several other String objects.

After (2), there are no references to the String object initially referenced by arg1.

The String object is now eligible for garbage collection.




PreviousNext

Related