Inner Classes - Quiz

Total: 8 questions

1. 

What is an inner class in java?

An inner class is a "class within a class" in java.

2. 

Types of inner classes in java.

  • Regular inner classes (or member inner classes)
  • Method-local inner classes
  • Anonymous inner classes
  • Static nested classes
3. Where "regular" inner class can be declared?
Inside another class, but outside any code block or method.
4. 

Street is an inner class of the Town. Update the code to create a legal instance of Street class. 

public static void main(String[] args) {
  Town town = new Town(); 
  ??? street= ???;
  street.seeTown();
}
public static void main(String[] args) {
  Town town = new Town(); 
  Town.Street street = town.new Street();
  street.seeTown();
}
5. 

Differences between instantiation of inner class within and outside of the outer class (but not static).

- For instantiation in the outer class, normal way of declaration should be used: Tail tail = new Tail();

- For instantiation outside of the outer class or in static method, name of the inner class should include the name of outer class: new Cat().new Tail(); or outerObjRef.new Tail();

6. 

Update //1 and //2 to print correct references to inner(Street.class) and outer(Town.class) classes.

public class Town {
    public String name = "Kharkiv";
    class Street{
        public void watchTown() {
            System.out.println("Town name is " + name);
            System.out.println("Street class ref is " + //1);
            System.out.println("Town class ref is " + //2);
        }
    }
}
public class Town {
    public String name = "Kharkiv";
    class Street{
        public void watchTown() {
            System.out.println("Town name is " + name);
            System.out.println("Street class ref is " + this);
            System.out.println("Town class ref is " + Town.this);
        }
    }
}
7. 

Where method-local inner class should be instantiated?

Within the same method (block), but after the class definition.

8. 

Where is the mistake? 

2. Kangaroo kangaroo = new Kangaroo() {
3.   public void movement() {
4.       System.out.println("jump");
5.   }
6. } 
7. Tiger tiger = new Tiger();

Missing the semicolon in the line 6.

Page 1 of 1