The output is going to be 2. The reason is that polymorphism means - dynamically figure out which method to call based on the actual instance type. So we start out with a new F. This is an F. It is marked as an F internally with a special marker.
Now we assign this to a D reference. This is fine- object references can point to the subclass or any base class. However, the object itself is an F. Later we could say ref1 = new D(); and that would be valid as well.
Now we tell it- we have another reference, this one is called ref2, and it points to something of type E. We assign it to the original object F. This is fine as well- the F is a subclass of E.
Now finally, we ask to call the function on ref2. It is pointing to the object of type F. It dynamically resolves this, and says- what is the function for F? The one that returns 2.