Hi Sagar,
But when I try these options all the options are compiled without errors.
Really? But it did give me errors, when I tried...
So why only AC ?
Considering option B)
I think, this method invocation's signature would be like findLarger(Integer, Double) and even if it did compile, would cause a ClassCastException when it tries to compare an Integer with a Double...
But the compiler even before that guesses that there is a possibility that
a Double can be returned by the method(in the else part) and that it cannot be assigned to an int without an explicit cast... and hence the compiler error...
Considering Option D)
int x = (int) t.findLarger(new Double(123), new Double(456));
Now here, if at all it executes, there wont be any problems with the comparison of a Double with a Double and there wont be any ClassCastException... But then the compiler sees that you are trying to cast a Double to an int which is not possible... (Double to double is ok, double to int is ok, but Double to int is not...) and hence the error.
Options A and C are fine, but still option A gives a ClassCastException on running since a String cannot be compared with an Integer... anws thats not a compiler error, so option A holds..
Regards,
Vishwa