Skip to content
QuiznExam

Inner Classes in Java

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.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is an inner class in Java?

    • A. A class declared inside another class or interface
    • B. A class that can only contain methods
    • C. A class that must be declared public
    • D. A class that cannot have objects
    Show answer

    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.

  2. 2. Which of the following is a type of nested class in Java?

    • A. Member inner class
    • B. Local class
    • C. Anonymous class
    • D. All of the above
    Show answer

    D. All of the above

    Java supports member inner classes, local classes, anonymous classes, and static nested classes.

  3. 3. Which type of nested class is declared with the static keyword?

    • A. Local class
    • B. Anonymous class
    • C. Static nested class
    • D. Method class
    Show answer

    C. Static nested class

    A static nested class is declared using the static keyword inside another class.

  4. 4. What is a member inner class?

    • A. A non-static class declared directly inside another class
    • B. A class declared only inside a method
    • C. A class that cannot access outer class members
    • D. A class that must always be abstract
    Show answer

    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.

  5. 5. How is an object of a non-static member inner class typically created from outside its outer class?

    • A. Outer.Inner obj = new Outer.Inner();
    • B. Outer.Inner obj = new Outer().new Inner();
    • C. Inner obj = new Inner();
    • D. new Inner.Outer();
    Show answer

    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.

  6. 6. Can a non-static inner class access members of its enclosing class?

    • A. Yes, including accessible private members
    • B. No, never
    • C. Only public members
    • D. Only static members
    Show answer

    A. Yes, including accessible private members

    An inner class has access to members of its enclosing class, including private members.

  7. 7. What is a static nested class?

    • A. A nested class that does not require an instance of the outer class to be instantiated
    • B. A class that can only contain static methods
    • C. A class that cannot access any outer class members
    • D. A class that must be declared outside the package
    Show answer

    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.

  8. 8. Which statement correctly describes a static nested class?

    • A. It can directly access only static members of the enclosing class
    • B. It automatically receives an outer class instance
    • C. It must always be private
    • D. It cannot contain methods
    Show answer

    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.

  9. 9. Where is a local class declared?

    • A. Inside a method, constructor, or initializer block
    • B. Only inside an interface
    • C. Only outside all classes
    • D. Inside a package declaration
    Show answer

    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.

  10. 10. What is an anonymous class in Java?

    • A. A class without a class name that is declared and instantiated in an expression
    • B. A class that has no methods
    • C. A class that must be static
    • D. A class that cannot implement interfaces
    Show answer

    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.

  11. 11. Which keyword is used to declare a static nested class?

    • A. nested
    • B. static
    • C. inner
    • D. local
    Show answer

    B. static

    The static keyword is placed before the nested class declaration to make it a static nested class.

  12. 12. Which statement about an anonymous class is correct?

    • A. It can extend a class or implement an interface
    • B. It must always have a declared class name
    • C. It cannot contain methods
    • D. It can implement multiple classes
    Show answer

    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.

  13. 13. What is one common use of anonymous classes in Java?

    • A. Creating a one-time implementation of an interface or class
    • B. Declaring package names
    • C. Replacing all constructors
    • D. Creating primitive data types
    Show answer

    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.

  14. 14. What is a key difference between a member inner class and a static nested class?

    • A. A member inner class has an implicit association with an outer instance, while a static nested class does not
    • B. A static nested class cannot have methods
    • C. A member inner class must always be public
    • D. There is no difference
    Show answer

    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.

  15. 15. What is a major benefit of using nested or inner classes?

    • A. They can logically group closely related implementation details
    • B. They automatically improve application performance
    • C. They eliminate the need for inheritance
    • D. They prevent all access to outer classes
    Show answer

    A. They can logically group closely related implementation details

    Nested classes can improve organization and encapsulation by keeping closely related implementation details together.