nerdexam
Oracle

1Z0-809 · Question #132

Given the code fragment: class Student { String name; int age; } And, public class Test { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); Student s3…

The correct answer is A. After line 11, one object is eligible for garbage collection. After s1 = s2 (line 11), s1 is redirected to point at the same object s2 references - meaning the original object created for s1 (call it Object A) now has zero references pointing to it, making it eligible for garbage collection. Only one object is orphaned at this exact line…

Question

Given the code fragment: class Student { String name; int age; } And, public class Test { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); s1 = s2; s3 = s1; s2 = null; } } Which statement is true?

Options

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

How the community answered

(25 responses)
  • A
    72% (18)
  • B
    16% (4)
  • C
    4% (1)
  • D
    8% (2)

Explanation

After s1 = s2 (line 11), s1 is redirected to point at the same object s2 references - meaning the original object created for s1 (call it Object A) now has zero references pointing to it, making it eligible for garbage collection. Only one object is orphaned at this exact line because s2 still holds Object B and s3 still holds Object C, so B and C remain reachable.

Why the distractors fail:

  • B (two objects) - that count only becomes accurate after line 12 (s3 = s1), which also orphans Object C; the question asks about line 11 specifically.
  • C (none) - Object A definitely loses all references when s1 is overwritten.
  • D (three objects) - Objects B and C still have live references at line 11, so they cannot be collected.

Memory tip: Draw a reference diagram - boxes for objects, arrows for variables. Every time a variable is reassigned, erase its old arrow. Any object with no incoming arrows is GC-eligible. Count the arrows, not the variables.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice