Five slots and shared references

A library prototype fills five storage slots with references, then replaces two entries. After the Java code finishes, how many distinct ordinary Object instances are referenced by the entries of slots? Exclude the array object itself. Give an exact whole-number count, with no decimal places.


Java
Object shared = new Object();
Object[] slots = new Object[5];

for (int i = 0; i < slots.length; i++) {
    slots[i] = shared;
}

slots[1] = new Object();
slots[4] = new Object();

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More The Object Class Questions: