250+ TOP MCQs on Nested Class and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Nested Class”.

1. Which among the following best describes a nested class?
a) Class inside a class
b) Class inside a function
c) Class inside a package
d) Class inside a structure
Answer: a
Clarification: If a class is defined inside another class, the inner class is termed as nested class. The inner class is local to the enclosing class. Scope matters a lot here.

2. Which feature of OOP reduces the use of nested classes?
a) Encapsulation
b) Inheritance
c) Binding
d) Abstraction
Answer: b
Clarification: Using inheritance we can have the security of the class being inherited. The subclass can access the members of parent class. And have more feature than a nested class being used.

3. How many categories are nested classes divided into?
a) 2
b) 3
c) 4
d) 5
Answer: a
Clarification: The nested classes are divided into two main categories. Namely, Static and non-static. The categories define how the classes can be used inside another class.

4. Non-static nested classes have access to _____________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) All the members
Answer: d
Clarification: The non-static nested class can access all the members of the enclosing class. All the data members and member functions can be accessed from the nested class. Even if the members are private, they can be accessed.

5. Static nested classes doesn’t have access to _________________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) Any other members
Answer: d
Clarification: The static nested class doesn’t have access to any other members of the enclosing class. This is a rule that is made to ensure that only the data which can be common to all the object is being accessed by the static nested class.

6. The nested class can be declared ___________________
a) Public
b) Private
c) Protected
d) Public, Protected, Private or Package private
Answer: d
Clarification: The nested class can be declared with any specifier, unlike the outer classes which can only be declared public or package private. This is flexibility given for the nested class being a member of enclosing class.

7. Use of nested class ____________ encapsulation.
a) Increases
b) Decreases
c) Doesn’t affect
d) Slightly decreases
Answer: a
Clarification: The use of nested class increases encapsulation as the inner class is getting even more grouped into the enclosing class. Firstly the class encapsulate the data, having nested classes can increase the encapsulation even further.

8. Which among the following is the correct advantage/disadvantage of nested classes?
a) Makes the code more complex
b) Makes the code unreadable
c) Makes the code efficient and readable
d) Makes the code multithreaded
Answer: c
Clarification: The use of nested classes makes the code more streamed towards a single concept. This allows to group the most similar and related classes together and makes it even more efficient and readable.

9. How to access static nested classes?
a) OuterClass.StaticNestedClass
b) OuterClass->StaticNestedClass
c) OuterClass(StaticNestedClass)
d) OuterClass[StaticNestedClass]
Answer: a
Clarification: Like any other member of the class, the static nested class uses the dot operator to be accessed. The reason behind is, the static classes can’t work with instances, hence we use enclosing class name to access static nested class.

10. A nested class can have its own static members.
a) True
b) False
Answer: b
Clarification: The nested classes are associated with the object of the enclosing class. Hence have direct access to the members of that object. Hence the inner class can’t have any static members of its own. Otherwise the rule of static members would be violated using enclosing class instance.

11. How to create object of the inner class?
a) OuterClass.InnerClass innerObject = outerObject.new InnerClass();
b) OuterClass.InnerClass innerObject = new InnerClass();
c) InnerClass innerObject = outerObject.new InnerClass();
d) OuterClass.InnerClass = outerObject.new InnerClass();
Answer: a
Clarification: An instance of inner class can exist only within instance of outer class. To instantiate the inner class, one must instantiate the outer class first. This can be done by the correct syntax above.

12. What will be the output of the following code?

public class Test
{
    public int a=0;
    class innerClass
    {
	public int a=1;
	void innermethod(int x)
	{
	    System.out.println(“value of x =+ x);
	    System.out.println(“value of this.x =+ this.x);
	    System.out.println(“value of Test.this.x =+ Test.T=this.x);
	}
    }
}	
public static void main( String args[] )
{
	Test t=new Test();
	Test.innerClass im=t.new innerClass();
	im.innermethod(55);
}

a)

  value of x = 55
  value of this.x = 0
  value of Test.this.x = 1

b)

  value of x = 1
  value of this.x = 0
  value of Test.this.x = 55

c)

  value of x = 55
  value of this.x = 1
  value of Test.this.x = 0

d)

  value of x = 0
  value of this.x = 55
  value of Test.this.x = 1

View Answer

Answer: c
Clarification: The variable x denotes the parameter of the function. And this.x is the variable of the inner class. Test.this.x is the variable of the outer class. Hence we get this output.

 
 

13. Instance of inner class can exist only _______________ enclosing class.
a) Within
b) Outside
c) Private to
d) Public to
Answer: a
Clarification: The class defined inside another class is local to the enclosing class. This means that the instance of inner class will not be valid outside the enclosing class. There is no restriction for instance to be private or public always.

14. If a declaration of a member in inner class has the same name as that in the outer class, then ________________ enclosing scope.
a) Outer declaration shadows inner declaration in
b) Inner declaration shadows outer declaration in
c) Declaration gives compile time error
d) Declaration gives runtime error
Answer: b
Clarification: The inner class will have more preference for its local members than those of the enclosing members. Hence it will shadow the enclosing class members. This process is known as shadowing.

15. A static nested class is _____________ class in behavior that is nested in another _________ class.
a) Top level, top level
b) Top level, low level
c) Low level, top level
d) Low level, low level
Answer: a
Clarification: Top level class encloses the other classes or have same preference as that of other top level classes. Having a class inside the top level class is indirectly having a top level class which higher degree of encapsulation.

Leave a Reply

Your email address will not be published. Required fields are marked *