Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Review Questions

8.1What will be the result of compiling and running the following program?
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 program will fail to compile because the class Inner cannot be declared within the class Outer.

  2. The program 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 program will fail to compile because the field secret is not accessible from the method getSecret().

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

  5. The code will compile and print 123, when run.

8.2Which statements about nested classes are true?

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. An instance of the outer class can be associated with many instances of a nonstatic member class.

8.3What will be the result of compiling and running the following program?
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 program will fail to compile because the static main() method attempts to create a new instance of the static member class State.

  2. The program will fail to compile because the class State.Memento is not accessible from the main() method.

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

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

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

  6. The program will compile and print 11, 18, and 11, when run.

8.4What will be the result of compiling and running 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 and print 2, 3, and 4, in that order, when run.

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

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

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

8.5Which statements about the following program are true?
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.

8.6What will be the result of compiling and running the following program?
public class Outer {
  private int innerCounter;

  class Inner {
    Inner() {innerCounter++;}
    public String toString() {
      return String.valueOf(innerCounter);
    }
  }

  private void multiply() {
    Inner inner = new Inner();
    this.new Inner();
    System.out.print(inner);
    inner = new Outer().new Inner();
    System.out.println(inner);
  }

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

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile but throw an exception when run.

  3. The program will compile and print 22, when run.

  4. The program will compile and print 11, when run.

  5. The program will compile and print 12, when run

  6. The program will compile and print 21, when run.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint