Java OCA OCP Practice Question 791

Question

Which of the following are true right before the main() method ends? (Choose two.)

public static void main(String[] args) { 
   String s1 = new String("A"); 
   String s2 = new String("B"); 
   String s3 = new String("C"); 
? 
   s1 = s2; 
   s2 = s3; 
   s3 = s1; 
} 
  • A. No objects are eligible for garbage collection.
  • B. One object is eligible for garbage collection.
  • C. Two objects are eligible for garbage collection.
  • D. No objects are guaranteed to be garbage collected.
  • E. One object is guaranteed to be garbage collected.
  • F. Two objects are guaranteed to be garbage collected.


B, D.

Note

At the end of the method, s1 and s3 both point to "B".

s2 points to "C".

Since there are no references to "A", it is eligible for garbage collection, making Option B correct.

Garage collection is not guaranteed to run, so Option D is also correct.




PreviousNext

Related