Java OCA OCP Practice Question 260

Question

How many of the String objects are eligible for garbage collection right before the end of the main method?

public static void main(String[] fruits) { 
        String fruit1 = new String("apple"); 
        String fruit2 = new String("orange"); 
        String fruit3 = new String("pear"); 
     ? 
        fruit3 = fruit1; 
        fruit2 = fruit3; 
        fruit1 = fruit2; 
} 
  • A. None
  • B. One
  • C. Two
  • D. Three


C.

Note

All three references point to the String apple.

This makes the other two String objects eligible for garbage collection and Option C correct.




PreviousNext

Related