Java Programming - Inner Classes

Why should I learn to solve Java Programming questions and answers section on "Inner Classes"?

Learn and practise solving Java Programming questions and answers section on "Inner Classes" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.

Where can I get the Java Programming questions and answers section on "Inner Classes"?

IndiaBIX provides you with numerous Java Programming questions and answers based on "Inner Classes" along with fully solved examples and detailed explanations that will be easy to understand.

Where can I get the Java Programming section on "Inner Classes" MCQ-type interview questions and answers (objective type, multiple choice)?

Here you can find multiple-choice Java Programming questions and answers based on "Inner Classes" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.

How do I download the Java Programming questions and answers section on "Inner Classes" in PDF format?

You can download the Java Programming quiz questions and answers section on "Inner Classes" as PDF files or eBooks.

How do I solve Java Programming quiz problems based on "Inner Classes"?

You can easily solve Java Programming quiz problems based on "Inner Classes" by practising the given exercises, including shortcuts and tricks.

Exercise : Inner Classes - General Questions
1.
Which is true about an anonymous inner class?
It can extend exactly one class and implement exactly one interface.
It can extend exactly one class and can implement multiple interfaces.
It can extend exactly one class or implement exactly one interface.
It can implement multiple interfaces regardless of whether it also extends a class.
Answer: Option
Explanation:

Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class).

Option A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer Option C.


2.
class Boo 
{
    Boo(String s) { }
    Boo() { }
}
class Bar extends Boo 
{
    Bar() { }
    Bar(String s) {super(s);}
    void zoo() 
    {
    // insert code here
    }
}
which one create an anonymous inner class from within class Bar?
Boo f = new Boo(24) { };
Boo f = new Bar() { };
Bar f = new Boo(String s) { };
Boo f = new Boo.Bar(String s) { };
Answer: Option
Explanation:

Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works.

Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class.

Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has.

Option D uses incorrect syntax.


3.
Which is true about a method-local inner class?
It must be marked final.
It can be marked abstract.
It can be marked public.
It can be marked static.
Answer: Option
Explanation:

Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful).

Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so).

C and D are incorrect because a method-local inner class cannot be made public (remember-you cannot mark any local variables as public), or static.


4.
Which statement is true about a static nested class?
You must have a reference to an instance of the enclosing class in order to instantiate it.
It does not have access to nonstatic members of the enclosing class.
It's variables and methods must be static.
It must extend the enclosing class.
Answer: Option
Explanation:

Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class).

Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class.

Option C is incorrect because static nested classes can declare and define nonstatic members.

Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.


5.
Which constructs an anonymous inner class instance?
Runnable r = new Runnable() { };
Runnable r = new Runnable(public void run() { });
Runnable r = new Runnable { public void run(){}};
System.out.println(new Runnable() {public void run() { }});
Answer: Option
Explanation:

D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.

A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation.

B and C use incorrect syntax.