Oracle
1Z0-819 · Question #197
How many Thing objects are eligible for garbage collection in line 1? ``java private String name; public Thing(String name) { this.name = name; } public String toString() { return name; } public…
The correct answer is D. 4. See the full explanation below for the reasoning.
Question
How many Thing objects are eligible for garbage collection in line 1? ```java
private String name;
public Thing(String name) {
this.name = name;
}
public String toString() {
return name;
}
public class Tester {
public static void main(String[] args) {
List<Thing> things = new ArrayList<>();
for (int i=0; i<5; i++) {
things.add(new Thing("Thing" + i));
}
System.out.println(things);
}
public static Thing[] processThings() {
Thing[] things = new Thing[3];
things[0] = new Thing("Rat");
things[1] = new Thing("Cat");
things[2] = things[0]; // Line 1
things[0] = new Thing("Dog");
things[1] = things[2];
return things;
}
}
Options
- A0
- B2
- C0
- D4
- E3
How the community answered
(32 responses)- A9% (3)
- B16% (5)
- C3% (1)
- D69% (22)
- E3% (1)
Community Discussion
No community discussion yet for this question.