To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) Compile fail due to error on line no 2 - This option is incorrect because there is no error on line 2. Line 2 is the definition of the method add
, which correctly takes two int
parameters and returns an int
value.
Option B) Compile fail due to error on line no 9 - This option is correct because there is an error on line 9. The add
method is being called without an instance of the class B
. To call a non-static method from a static context, an instance of the class is needed. In this case, the main
method is static, and it is trying to call the add
method directly without creating an object of class B
.
Option C) Compile fail due to error on line no 8 - This option is incorrect because there is no error on line 8. Line 8 declares a variable s
of type short
and assigns it a value of 9. This is a valid statement.
Option D) 15 - This option is incorrect because the code will not compile. Hence, there won't be any output.
The correct answer is B. Compile fail due to error on line no 9 because the code fails to compile due to the error on line 9.