🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: JVM Memory and Garbage Collection: all 10 questions

Quiz: JVM Memory and Garbage Collection · Question 6 of 10

In this code, how many Box objects are eligible for GC at the end of main's body? (code)

In this code, how many Box objects are eligible for GC at the end of main's body?

Box x = new Box();   // object 1
Box y = new Box();   // object 2
x = y;
y = null;
  1. A.0
  2. B.2
  3. C.1
  4. D.It cannot be known
Answer & explanation

Correct answer: 1

Object 1 lost its only reference when x was pointed to object 2. Object 2 is still reachable through x.

Can you get the rest right without peeking?

Take the timed Quiz: JVM Memory and Garbage Collection quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →