Practice Test Question:
Consider the following code:
String entree = new String (“chicken”);
String side = “salad”;
entree = “turkey”;
String dessert;
entree = side;
String extra = entree + side;
dessert = “pie”;
How many String objects were created, and how many are accessible at the end?
How many aliases are present, and is there any garbage?
My logic: 3 literals created, one String with the new operator, and one concatenating entree and side, so 5 total objects.
dessert and extra is 2 objects, side and the 3rd assigning of entree. So 4 objects are accessible out of the 5 total created.
1 alias, entree refers to side.
Garbage, entree lost references to "turkey" and "chicken".
Could you help me assess my thought process on this question?