Object Oriented Programming using C++ Questions and Answers – Nested Class

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

4. Non-static nested classes have access to _____________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) All the members
View Answer

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. The nested class can be declared ___________________
a) Public
b) Private
c) Protected
d) Public, Protected, Private or Package private
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement

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
View Answer

Answer: c
Explanation: 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]
View Answer

Answer: a
Explanation: 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.
advertisement

10. A nested class can have its own static members.
a) True
b) False
View Answer

Answer: b
Explanation: 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();
View Answer

Answer: a
Explanation: 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
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.