nerdexam
Oracle

1Z0-808 · Question #93

Given the code fragment: Which statement is true? ``java Class Student { String name; int age; } And, 1. public class Test { 2. public static void main(String[] args) { 3. Student s1 = new…

The correct answer is C. After line 8, one object is eligible for garbage collection. Run the code class Student { String name; int age; } // Called by the garbage collector on an object when garbage collection determines // that there are no more references to the object. @Override protected void finalize () { System.out.println("Finalized Object\n"); } public…

Java Class Design

Question

Given the code fragment: Which statement is true?
Class Student {
String name;
int age;
}

And,

1. public class Test {
2. public static void main(String[] args) {
3. Student s1 = new Student();
4. Student s2 = new Student();
5. Student s3 = new Student();
6. s1 = s3;
7. s3 = s2;
8. s2 = null;
9. }
10. }

Options

  • AAfter line 8, three objects are eligible for garbage collection.
  • BAfter line 8, two objects are eligible for garbage collection.
  • CAfter line 8, one object is eligible for garbage collection.
  • DAfter line 8, none of the objects are eligible for garbage collection.

How the community answered

(59 responses)
  • A
    14% (8)
  • B
    3% (2)
  • C
    75% (44)
  • D
    8% (5)

Explanation

Run the code class Student { String name; int age; }

// Called by the garbage collector on an object when garbage collection determines // that there are no more references to the object. @Override protected void finalize () { System.out.println("Finalized Object\n"); }

public class Main { public static void main(String[] args) throws InterruptedException { Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student();

s1 = s3;
s2 = s1;
s2 = null;

System.gc();
Thread.sleep(3000);

System.out.println(s1.name);
System.out.println(s3.name);

} }

The output is Finalized Object null null

In other words, s2 has been garbage collected. s1 and s3 haven't been garbage collected.

Topics

#garbage collection#object references#null assignment#memory management

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice