🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.1 2
  2. B.2 2
  3. C.1 1
  4. 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 →