0

Java inner classes Quiz

Description: Java inner classes Quiz
Number of Questions: 4
Created by:
Tags: java
Attempted 0/4 Correct 0 Score 0
  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

Which is true about a method-local inner class?

  1. It must be marked final.

  2. It can be marked abstract.

  3. It can be marked public.

  4. It can be marked static.


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) It must be marked final - This option is incorrect. Method-local inner classes do not have any restrictions on being marked as final. They can be marked as final, but it is not a requirement.

Option B) It can be marked abstract - This option is correct. Method-local inner classes can be marked as abstract. This allows the class to have abstract methods that must be implemented by any non-abstract subclasses.

Option C) It can be marked public - This option is incorrect. Method-local inner classes cannot be marked as public. They can only have either default (package-private) or private access modifiers.

Option D) It can be marked static - This option is incorrect. Method-local inner classes cannot be marked as static. They are implicitly associated with an instance of the enclosing class and cannot exist without an instance of the enclosing class.

Therefore, the correct answer is B) It can be marked abstract. This option is correct because method-local inner classes can be marked as abstract and have abstract methods.

Please select the most appropriate option.

  1. You must have a reference to an instance of the enclosing class in order to instantiate it.

  2. It does not have access to nonstatic members of the enclosing class.

  3. It's variables and methods must be static.

  4. It must extend the enclosing class.


Correct Option: B
Explanation:

To answer this question, you need to have an understanding of nested classes in Java.

A static nested class is a class that is defined inside another class, and it is marked as static. Here is an explanation of each option:

A. You must have a reference to an instance of the enclosing class in order to instantiate it. This statement is false. Unlike an inner class, a static nested class does not require an instance of the enclosing class to be instantiated. You can create an instance of a static nested class without having an instance of the enclosing class.

B. It does not have access to nonstatic members of the enclosing class. This statement is true. Since a static nested class is static, it does not have access to nonstatic members (variables or methods) of the enclosing class. It can only access static members of the enclosing class.

C. Its variables and methods must be static. This statement is false. Although the static nested class is static, it can have both static and nonstatic variables and methods. It is not required for all of its members to be static.

D. It must extend the enclosing class. This statement is false. A static nested class does not have to extend the enclosing class. It is a separate class and can have its own inheritance hierarchy.

Based on the explanations above, the correct statement about a static nested class is:

The Answer is: B. It does not have access to nonstatic members of the enclosing class.

Which constructs an anonymous inner class instance?

  1. Runnable r = new Runnable() { };

  2. Runnable r = new Runnable(public void run() { });

  3. Runnable r = new Runnable { public void run(){}};

  4. System.out.println(new Runnable() {public void run() { }});


Correct Option: D
- Hide questions