← Quiz: Shallow and Deep Copy: all 10 questions
Quiz: Shallow and Deep Copy · Question 4 of 10
What does this print? (code)
What does this print?
import java.util.*;
class Team implements Cloneable {
List<String> members = new ArrayList<>();
@Override
public Team clone() {
try { return (Team) super.clone(); }
catch (CloneNotSupportedException e) { throw new AssertionError(e); }
}
}
public class Main {
public static void main(String[] args) {
Team t1 = new Team();
t1.members.add("Asha");
Team t2 = t1.clone();
t2.members.add("Ravi");
System.out.println(t1.members.size() + " " + t2.members.size());
}
}
- A.1 2
- B.2 2
- C.1 1
- D.2 1
Answer & explanation
Correct answer: 2 2
The clone is shallow: both Teams share one ArrayList, so it holds 2 names for both.
Can you get the rest right without peeking?
Take the timed Quiz: Shallow and Deep Copy quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →