Review Questions

graphics/rq_icon.gif

7.1

What will be the result of attempting to compile and run the following code?

public class MyClass {
    public static void main(String[] args) {
        Outer objRef = new Outer();
        System.out.println(objRef.createInner().getSecret());
    }
}

class Outer {
    private int secret;
    Outer() { secret = 123; }

    class Inner {
        int getSecret() { return secret; }
    }

    Inner createInner() { return new Inner(); }
}

Select the one correct answer.

  1. The code will fail to compile because the class Inner cannot be declared within the class Outer.

  2. The code will fail to compile because the method createInner() cannot be allowed to pass objects of the class Inner to methods outside of the class Outer.

  3. The code will fail to compile because the secret field is not accessible from the method getSecret().

  4. The code will fail to compile because the method getSecret() is not visible from the main() method in the class MyClass.

  5. The code will compile without error and will print 123 when run.

7.2

Which statements are true about nested classes?

Select the two correct answers.

  1. An instance of a static member class has an inherent outer instance.

  2. A static member class can contain non-static fields.

  3. A static member interface can contain non-static fields.

  4. A static member interface has an inherent outer instance.

  5. For each instance of the outer class, there can exist many instances of a non-static member class.

7.3

What will be the result of attempting to compile and run the following code?

public class MyClass {
    public static void main(String[] args) {
        State st = new State();
        System.out.println(st.getValue());
        State.Memento mem = st.memento();
        st.alterValue();
        System.out.println(st.getValue());
        mem.restore();
        System.out.println(st.getValue());
    }

    public static class State {
        protected int val = 11;

        int getValue() { return val; }
        void alterValue() { val = (val + 7) % 31; }
        Memento memento() { return new Memento(); }

        class Memento {
            int val;

            Memento() { this.val = State.this.val; }
            void restore() { ((State) this).val = this.val; }
        }
    }
}

Select the one correct answer.

  1. The code will fail to compile since the static main() method tries to create a new instance of the static member class State.

  2. The code will fail to compile since the declaration of class State.Memento is not accessible from the main() method.

  3. The code will fail to compile since the non-static member class Memento declares a field with the same name as a field in the outer class State.

  4. The code will fail to compile since the State.this.val expression in the Memento constructor is invalid.

  5. The code will fail to compile since the ((State) this).val expression in the method restore() of the class Memento is invalid.

  6. The program compiles without error and prints 11, 18, and 11 when run.

7.4

What will be the result of attempting to compile and run the following program?

public class Nesting {
    public static void main(String[] args) {
        B.C obj = new B().new C();
    }
}

class A {
    int val;
    A(int v) { val = v; }
}

class B extends A {
    int val = 1;
    B() { super(2); }

    class C extends A {
        int val = 3;
        C() {
            super(4);
            System.out.println(B.this.val);
            System.out.println(C.this.val);
            System.out.println(super.val);
        }
    }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile without error and print 2, 3, and 4, in that order, when run.

  3. The program will compile without error and print 1, 4, and 2, in that order, when run.

  4. The program will compile without error and print 1, 3, and 4, in that order, when run.

  5. The program will compile without error and print 3, 2, and 1, in that order, when run.

7.5

Which statements are true about the following program?

public class Outer {
    public void doIt() {
    }
    public class Inner {
        public void doIt() {
        }
    }
    public static void main(String[] args) {
        new Outer().new Inner().doIt();
    }
}

Select the two correct answers.

  1. The doIt() method in the Inner class overrides the doIt() method in the Outer class.

  2. The doIt() method in the Inner class overloads the doIt() method in the Outer class.

  3. The doIt() method in the Inner class hides the doIt() method in the Outer class.

  4. The full name of the Inner class is Outer.Inner.

  5. The program will fail to compile.