Java Inner Classes Quiz
12 Questions
100 Views

Java Inner Classes Quiz

Created by
@EffortlessGyrolite7402

Questions and Answers

Which are true about a static nested class? (Choose all that apply)

  • It does not have access to non-static members of the enclosing class (correct)
  • You must have a reference to an instance of the enclosing class in order to instantiate it
  • It must extend the enclosing class
  • Its variables and methods must be static
  • If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner(); (correct)
  • Which create an anonymous inner class from within class Bar? (Choose all that apply)

  • Boo f = new Boo(24) { };
  • Bar f = new Boo(String s) { };
  • Boo f = new Bar() { }; (correct)
  • Boo f = new Boo() {String s; }; (correct)
  • Boo f = new Boo.Bar(String s) { };
  • Which are true about a method-local inner class? (Choose all that apply)

  • It can be marked static
  • It can access private members of the enclosing class (correct)
  • It can be marked abstract (correct)
  • It can be marked public
  • It must be marked final
  • What is the result of the given code? (Choose one)

    <p>Compilation fails because of an error on a line other than 3, 4, or 8</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>Compilation fails because of an error on line 10</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>57 22</p> Signup and view all the answers

    Which, inserted independently at line 6, compile and produce the output 'spooky'? (Choose all that apply)

    <p>Cathedral.Sanctum s = c.new Sanctum();</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>middle</p> Signup and view all the answers

    Which, inserted independently at line 5, produce the output 'hi'? (Choose all that apply)

    <p>{ Car.this.drive(); }</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>No output is produced</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>21</p> Signup and view all the answers

    What is the result of the given code? (Choose one)

    <p>Compilation fails</p> Signup and view all the answers

    Study Notes

    Static Nested Classes

    • Static nested classes do not require a reference to an instance of the enclosing class to be instantiated.
    • They cannot access non-static members of the enclosing class, similar to static methods.
    • The instantiation syntax for a static nested class includes enclosing class name, e.g., new MyOuter.MyInner();.
    • Static nested classes are not inherently required to extend the enclosing class.

    Anonymous Inner Classes

    • Anonymous inner classes can express polymorphism, allowing a superclass reference to refer to a subclass instance.
    • Correct instantiation involves using valid constructors as per defined class constructors.
    • Syntax should follow Java standards to avoid compilation errors.

    Method-local Inner Classes

    • Method-local inner classes can be declared abstract, but require subclasses to be instantiated.
    • They have access to all members of the enclosing class, including private members.
    • Local inner classes within methods cannot be marked public or static.

    Compilation and Runtime Errors

    • Proper syntax is crucial; missing semicolons or incorrect method signatures can lead to compilation errors.
    • Object references made through superclass types can only access defined members of the superclass.

    Class Scope and Access Modifiers

    • Inner class declarations impact method scope; a local inner class cannot overshadow an outer class declaration.
    • Accessing outer class methods must be utilized with correct syntax, depending on context (e.g., from an initialization block).

    Exception Handling

    • Proper exception handling is important in method overrides; checking for exceptions ensures safe execution of methods.
    • Inner classes can redefine methods from their enclosing classes and must correctly rethrow exceptions when required.

    Access to Private Members

    • Inner classes have special access privileges to the private members of the enclosing class.
    • This includes both instance and static variables of the enclosing class.

    Comparator Usage

    • To use an inner class implementing an interface like Comparator in static contexts, the inner class must be declared static.
    • The use of sorting functions will depend on correctly implementing the compare method within the inner class.

    Key Results

    • Compilation errors can arise from improper references, incorrect inner class declarations, or logical issues.
    • Understanding the scope, visibility, and syntax of classes, especially nested and inner classes, is essential for Java programming.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on Java Inner Classes, including static nested classes, anonymous inner classes, and method-local inner classes. This quiz will cover their characteristics, instantiation syntax, and access to class members.

    More Quizzes Like This

    Use Quizgecko on...
    Browser
    Browser