Java Assertions Quiz – 20 Questions with Answers
20 questions · 15 min · Medium
Test your knowledge of inner classes in Java, including member inner classes, static nested classes, local classes, anonymous classes, access to outer class members, and object creation.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. A class declared inside another class or interface
A nested class is declared within another class or interface. A non-static nested class is commonly referred to as an inner class.
D. All of the above
Java supports member inner classes, local classes, anonymous classes, and static nested classes.
C. Static nested class
A static nested class is declared using the static keyword inside another class.
A. A non-static class declared directly inside another class
A member inner class is a non-static nested class declared as a member of its enclosing class.
B. Outer.Inner obj = new Outer().new Inner();
A non-static member inner class instance is associated with an instance of its outer class, so an outer object is required for this form of creation.
A. Yes, including accessible private members
An inner class has access to members of its enclosing class, including private members.
A. A nested class that does not require an instance of the outer class to be instantiated
A static nested class is associated with the outer class rather than with a particular outer class instance.
A. It can directly access only static members of the enclosing class
A static nested class does not have an implicit reference to an outer class instance, so it cannot directly access instance members of the enclosing class.
A. Inside a method, constructor, or initializer block
A local class is declared within a block such as a method body, constructor body, or initializer.
A. A class without a class name that is declared and instantiated in an expression
An anonymous class has no explicit name and is commonly used for a one-time implementation of a class or interface.
B. static
The static keyword is placed before the nested class declaration to make it a static nested class.
A. It can extend a class or implement an interface
An anonymous class can extend one class or implement one interface and can provide or override methods as needed.
A. Creating a one-time implementation of an interface or class
Anonymous classes are useful when a small, one-time implementation is needed without creating a separately named class.
A. A member inner class has an implicit association with an outer instance, while a static nested class does not
A non-static member inner class is associated with an instance of its enclosing class, while a static nested class is not.
A. They can logically group closely related implementation details
Nested classes can improve organization and encapsulation by keeping closely related implementation details together.