Inner and Nested Classes — Concept Check
Questions on Inner and Nested Classes. Java supports four types of nested classes — static nested, inner, local, and anonymous — each with different scoping and access rules.
Questions in this quiz
8 questions on Inner and Nested Classes — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You are designing a utility class in Java. Which type of nested class should you use to avoid holding a reference to the enclosing instance?
- ALocal class
- BStatic nested class
- CInner class
- DAnonymous class
2.A Java developer notices a memory leak in their application. They find that an inner class is preventing garbage collection of its enclosing instance. Why is this happening?
- AThe inner class holds a reference to the enclosing instance.
- BThe inner class is defined inside a method, causing retention.
- CThe inner class is anonymous, which causes leaks.
- DThe inner class is static and thus retains memory.
3.In a Java application, you want to create a one-time use class with a specific method implementation. Which nested class type is most appropriate?
- AAnonymous class
- BLocal class
- CInner class
- DStatic nested class
4.A developer is using a static nested class to implement the Builder pattern. What can this class access from the enclosing class?
- ANeither static nor instance members of the enclosing class
- BOnly static members of the enclosing class
- CBoth static and instance members of the enclosing class
- DOnly instance members of the enclosing class
5.You have a method that requires a helper class to process data. This class needs to access final variables from the method. Which nested class type should you use?
- AInner class
- BLocal class
- CStatic nested class
- DAnonymous class
6.A developer incorrectly believes that static nested classes can access instance variables of the enclosing class. Why is this belief incorrect?
- AStatic nested classes are always abstract.
- BStatic nested classes are only used for anonymous implementations.
- CStatic nested classes do not hold a reference to the enclosing instance.
- DStatic nested classes are only for utility methods.
7.You are iterating over a LinkedList using an inner class. What is a potential risk associated with this approach?
- AIncreased complexity due to static context
- BInability to access static members of the enclosing class
- CMemory leaks due to reference to the enclosing instance
- DDifficulty in accessing local method variables
8.Which type of nested class in Java is most likely to cause a memory leak if not used carefully?
- AInner class
- BLocal class
- CStatic nested class
- DAnonymous class