🎙 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 1 of 10

What does this print? (code)

What does this print?

class Box {
    int value;
    Box(int value) { this.value = value; }
}

public class Main {
    public static void main(String[] args) {
        Box a = new Box(5);
        Box b = a;
        b.value = 10;
        a = new Box(20);
        System.out.println(a.value + " " + b.value);
    }
}
  1. A.10 10
  2. B.20 20
  3. C.20 10
  4. D.5 10
Answer & explanation

Correct answer: 20 10

b still points to the first Box (changed to 10); a was re-pointed to a new Box with 20.

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 →