Which is true about an anonymous inner class?

  1. It can extend exactly one class and implement exactly one interface.

  2. It can extend exactly one class and can implement multiple interfaces.

  3. It can extend exactly one class or implement exactly one interface.

  4. It can implement multiple interfaces regardless of whether it also extends a class.


Correct Option: C
Explanation:

To answer this question, one needs to understand the concept of an anonymous inner class in Java.

An anonymous inner class is a class that is defined and instantiated at the same time, without explicitly giving it a name. It is typically used when you need to create a class that is used only once and does not need to be reused.

Now let's go through each option and explain why it is right or wrong:

A. It can extend exactly one class and implement exactly one interface. This option is incorrect. An anonymous inner class can extend a class or implement an interface, but it cannot do both at the same time. It can either extend a class or implement an interface, but not both simultaneously.

B. It can extend exactly one class and can implement multiple interfaces. This option is incorrect. As mentioned earlier, an anonymous inner class cannot extend a class and implement multiple interfaces at the same time. It can either extend a class or implement an interface, but not both simultaneously.

C. It can extend exactly one class or implement exactly one interface. This option is correct. An anonymous inner class can either extend a class or implement an interface. It cannot do both at the same time, but it can choose to extend a class or implement an interface based on the requirements.

D. It can implement multiple interfaces regardless of whether it also extends a class. This option is incorrect. An anonymous inner class can implement multiple interfaces, but only if it does not extend a class. If it extends a class, it can only implement one interface.

Therefore, the correct answer is:

The Answer is: C

Find more quizzes: