If f() is a static method, which f() is called is determined by the compiler based on x's declared type.
As for a second case, if f() is declared final in the class that x is declared to be. Heh, that might take an example.
I would expect that line 2 will be compiled as a polymorphic lookup of f() since it could be A.f() or B.f() based on the instance x references at runtime.
However, line 5 should be compiled to directly call B.f() since that's the only option for a C (or any subclass of it).
Of course, IANAJVMA, so YMMV.