Which of the below given statements(s) is/are true?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which of the below given statements(s) is/are true?
I) To instantiate an instance of inner class, there should be a live instance of outer class.
II) An inner class instance can be created only from an outer class instance.
III) An inner class shares a special relationship with an instance of the enclosing class.
Show answer & explanation
All three statements accurately describe non-static inner classes in Java. Statement I is true: a non-static inner class requires a live instance of the outer class to be instantiated. Statement II is true: inner class instances can only be created via an outer class instance (e.g., outerInstance.new InnerClass()). Statement III is true: inner classes maintain an implicit reference to their enclosing class instance, enabling direct access to outer class members.
Step-by-step Derivation:
Analysis of non-static inner class behavior in Java:
Statement I - TRUE: Non-static inner classes are bound to an instance of the outer class. You cannot create an inner class instance without an existing outer class instance:
class Outer {
class Inner {}
}
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // Requires 'outer' instance
Statement II - TRUE: Inner class instances cannot be created independently. They must be instantiated through an outer class instance:
Outer.Inner inner = new Outer().new Inner(); // Created from outer instance
// NOT: Inner inner = new Inner(); // This fails—no outer context
Statement III - TRUE: Non-static inner classes hold an implicit reference to their enclosing class instance:
class Outer {
int x = 10;
class Inner {
void display() {
System.out.println(x); // Direct access to outer's member via implicit 'this' reference
}
}
}
All three statements correctly characterize non-static inner class semantics. Answer: A