Which is true? (Choose all that apply.)

  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces


Correct Option: C

AI Explanation

To answer this question, we need to understand the concept of inheritance in object-oriented programming.

In object-oriented programming, the keyword "extends" is used to establish an inheritance relationship between classes and interfaces. Here are the explanations for each option:

Option A) "X extends Y" is correct if and only if X is a class and Y is an interface. This option is incorrect. In Java, a class can extend another class or implement an interface. The keyword "extends" is used for class inheritance, while the keyword "implements" is used for interface implementation.

Option B) "X extends Y" is correct if and only if X is an interface and Y is a class. This option is incorrect. In Java, an interface cannot extend a class. It can only extend another interface using the keyword "extends".

Option C) "X extends Y" is correct if X and Y are either both classes or both interfaces. This option is correct. In Java, a class can extend another class, and an interface can extend another interface. Therefore, "X extends Y" is correct if X and Y are either both classes or both interfaces.

Option D) "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces. This option is incorrect. As explained above, "X extends Y" is not correct for all combinations. It is only correct if X and Y are either both classes or both interfaces.

Therefore, the correct answer is option C.

Find more quizzes: