Test Series - oops

Test Number 24/78

Q: 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
Solution: 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.
Q: Static nested classes doesn’t have access to _________________ from enclosing class.
A. Private members
B. Protected members
C. Public members
D. Any other members
Solution: 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.
Q: The nested class can be declared ___________________
A. Public
B. Private
C. Protected
D. Public, Protected, Private or Package private
Solution: 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.
Q: How to access static nested classes?
A. OuterClass.StaticNestedClass
B. OuterClass->StaticNestedClass
C. OuterClass(StaticNestedClass)
D. OuterClass[StaticNestedClass]
Solution: 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.
Q: Use of nested class ____________ encapsulation.
A. Increases
B. Decreases
C. Doesn’t affect
D. Slightly decreases
Solution: 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.
Q: 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
Solution: 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.
Q: 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();
Solution: 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.

You Have Score    /7