What is the output for the below code ? 1. public class A { 2. int add(int i, int j){ 3. return i+j; 4. } 5.} 6.public class B extends A{ 7. public static void main(String argv[]){ 8. short s = 9; 9. System.out.println(add(s,6)); 10. } 11.}

  1. Compile fail due to error on line no 2

  2. Compile fail due to error on line no 9

  3. Compile fail due to error on line no 8

  4. 15


Correct Option: B

AI Explanation

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.

Find more quizzes: