Bird
0
0

Given this code:

medium📝 Predict Output Q5 of 15
Java - Memory Management Basics
Given this code:
String s1 = new String("hello");
String s2 = s1;
s1 = null;
System.gc();
Which object is eligible for garbage collection?
ANo object is eligible
BThe String object "hello"
CThe reference s2
DThe reference s1
Step-by-Step Solution
Solution:
  1. Step 1: Identify object references

    The String object "hello" is referenced by s2 after s1 is set to null.
  2. Step 2: Determine eligibility for garbage collection

    Since s2 still references the object, it is not eligible for garbage collection.
  3. Final Answer:

    No object is eligible -> Option A
  4. Quick Check:

    Object with active references is not collected [OK]
Quick Trick: Objects with any reference are not collected [OK]
Common Mistakes:
  • Assuming object is collected when one reference is null
  • Confusing references with objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes