Self Test


The following questions will help you measure your understanding of the dynamic and life-altering material presented in this chapter. Read all of the choices carefully. Take your time. Breathe.

1. 

Given:

 public class MyOuter {    public static class MyInner { public static void foot) { } } } 

Which, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

  1. MyOuter.MyInner m = new MyOuter.Mylnner();

  2. MyOuter.MyInner mi = new MyInner();

  3. MyOuter m = new Myouter();

    MyOuter.MyInner mi = m.new Myouter.MyInner();

  4. MyInner mi = new MyOuter.MyInner();

image from book

2. 

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

  1. You must have a reference to an instance of the enclosing class in order to instantiate it.

  2. It does not have access to non-static members of the enclosing class.

  3. Its variables and methods must be static.

  4. If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner();.

  5. It must extend the enclosing class.

image from book

3. 

Given:

 public interface Runnable { void run(); } 

Which construct an anonymous inner class instance? (Choose all that apply.)

  1. Runnable r = new Runnable() { };

  2. Runnable r = new Runnable(public void run() { });

  3. Runnable r = new Runnable { public void run(){}};

  4. Runnable r = new Runnable() {public void run{}};

  5. System.out.println(new Runnable() (public void run() { }});

  6. System.out.println(new Runnable(public void run() {}));

image from book

4. 

Given:

 class Boo {    Boo(String s) { }    Boo() { } } class Bar extends Boo {    Bar() { }    Bar(String s) (super(s);}    void zoo() {    // insert code here    } } 

Which create an anonymous inner class from within class Bar? (Choose all that apply.)

  1. Boo f = new Boo(24) { };

  2. Boo f = new Bar() { };

  3. Boo f = new Boo() (String s; };

  4. Bar f = new Boo(String s) { };

  5. Boo f = new Boo.Bar(String s) { } ;

image from book

5. 

Given:

 1. class Foo { 2.   class Bar{ } 3. }  4. class Test { 5.    public static void main(String[] args) { 6.       Foo f = new Foo(); 7.       // Insert code here 8.    } 9. } 

Which, inserted at line 7, creates an instance of Bar? (Choose all that apply.)

  1. Foo.Bar b = new Foo.Bar();

  2. Foo.Bar b = f.new Bar () ;

  3. Bar b = new f.Bar();

  4. Bar b = f.new Bar();

  5. Foo.Bar b = new f.Bar();

image from book

6. 

Which are true about a method-local inner class? (Choose all that apply.)

  1. It must be marked final.

  2. It can be marked abstract.

  3. It can be marked public.

  4. It can be marked static.

  5. It can access private members of the enclosing class.

image from book

7. 

Which are true about an anonymous inner class? (Choose all that apply.)

  1. It can extend exactly one class and implement exactly one interface.

  2. It can extend exactly one class and can implement multiple interfaces.

  3. It can extend exactly one class or implement exactly one interface.

  4. It can implement multiple interfaces regardless of whether it also extends a class.

  5. It can implement multiple interfaces if it does not extend a class.

image from book

8. 

Given:

 public class Foo {    Foo() {System.out.print("foo");}    class Bar{       Bar() {System.out.print("bar");}       public void go() {System.out.print("hi");}    }    public static void main(String[] args) {       Foo f = new Foo ();       f.makeBar();    }    void makeBar() {      (new Bar() {}).go();    } } 

What is the result?

  1. Compilation fails.

  2. An error occurs at runtime.

  3. foobarhi

  4. barhi

  5. hi

  6. foohi

image from book

9. 

Given:

  1. public class TestObj {  2.   public static void main(String[] args) {  3.     Object o = new Object() {  4.       public boolean equals(Object obj) {  5.         return true;  6.       }  7.     }  8.     System.out.println(o.equals("Fred"));  9.   } 10. } 

What is the result?

  1. An exception occurs at runtime.

  2. true

  3. Fred

  4. Compilation fails because of an error on line 3.

  5. Compilation fails because of an error on line 4.

  6. Compilation fails because of an error on line 8.

  7. Compilation fails because of an error on a line other than 3, 4, or 8.

image from book

10. 

Given:

  1. public class HorseTest {  2.   public static void main(String [] args) {  3.    class Horse {  4.       public String name;  5.       public Horse(String s) {  6.         name = s;  7.       }  8.     }  9.     Object obj = new Horse("Zippo"); 10.     Horse h = (Horse) obj; 11.     System.out.println(h.name); 12.   } 13. } 

What is the result?

  1. An exception occurs at runtime at line 10.

  2. Zippo

  3. Compilation fails because of an error on line 3.

  4. Compilation fails because of an error on line 9.

  5. Compilation fails because of an error on line 10.

  6. Compilation fails because of an error on line 11.

image from book

11. 

Given:

  1. public class HorseTest {  2.   public static void main(String[] args) {  3.     class Horse {  4.       public String name;  5.       public Horse(String s) {  6.         name = s;  7.       }  8.     }  9.     Object obj = new Horse("Zippo"); 10.     System.out.println(obj.name); 11.   } 12. } 

What is the result?

  1. An exception occurs at runtime at line 10.

  2. Zippo

  3. Compilation fails because of an error on line 3.

  4. Compilation fails because of an error on line 9.

  5. Compilation fails because of an error on line 10.

image from book

12. 

Given:

 public abstract class AbstractTest {    public int getNum() {       return 45;    }    public abstract class Bar {      public int getNum() {        return 38;      }    }    public static void main(String[] args) {       AbstractTest t = new AbstractTest() {          public int getNum() {            return 22;          }        };       AbstractTest.Bar f = t.new Bar() {          public int getNum() {            return 57;           }        };        System.out.println(f.getNum() + " " + t.getNum());     } } 

What is the result?

  1. 57 22

  2. 45 38

  3. 45 57

  4. An exception occurs at runtime.

  5. Compilation fails.

image from book

Answers

1. 

þ  

A is correct. Mylnner is a static nested class, so it must be instantiated using the fully scoped name of MyOuter.My Inner.

ý  

B is incorrect because it doesn't use the enclosing name in the new. C is incorrect because it uses incorrect syntax. When you instantiate a nested class by invoking new on an instance of the enclosing class, you do not use the enclosing name. The difference between A and C is that C is calling new on an instance of the enclosing class rather than just new by itself. D is incorrect because it doesn't use the enclosing class name in the variable declaration.

2. 

þ  

B and D. B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the non-static members of the class (just as a static method can't access non-static members of a class). D uses the correct syntax for instantiating a static, nested class.

ý  

A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. C is incorrect because static nested classes can declare and define non-static members. E is wrong becauseit just is. There's no rule that says an inner or nested class has to extend anything.

3. 

þ  

E is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, it must override the run(), method of Runnable.

ý  

A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation. B, C, and D use incorrect syntax.

4. 

þ  

B and C. B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. C uses correct syntax for creating an instance of Boo.

ý  

A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. D is incorrect because it violates the rules of polymorphism; you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass doesn't have everything the subclass has. E uses incorrect syntax.

5. 

þ  

B is correct because the syntax is correct—using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class.

ý  

A, C, D, and E all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the call to new. C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong. D is incorrect because it doesn't use the enclosing class name in the reference variable declaration. E is incorrect because the new syntax is wrong.

6. 

þ  

B and E. B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful). E is correct because a method-local inner class works like any other inner class—it has a special relationship to an instance of the enclosing class, thus it can access all members of the enclosing class.

ý  

A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so). C and D are incorrect because a method-local inner class cannot be made public (remember—local variables can't be public) or static.

7. 

þ  

C is correct because the syntax of an anonymous inner class allows for only one.named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class:(in which case the anonymous class extends that one class).

ý  

A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer C.

8. 

þ  

C is correct because first the Foo instance is created, which means the Foo constructor runs and prints foo. Next, the makeBar() method is invoked, which creates a Bar, which means the Bar constructor runs and prints bar, and finally an instance is created (of an anonymous subtype of Bar), from which the go() method is invoked. Note that the line (new Bar() {}).go(); creates a little tiny anonymous inner class, a subtype of Bar.

ý  

A C, D, E, and F are incorrect based on the program logic described above.

9. 

þ  

G. This code would be legal if line 7 ended with a semicolon. Remember that line 3 is a statement that doesn't end until line 7, and a statement needs a closing semicolon!

ý  

A, B, C, D, E, and F are incorrect based on the program logic described above. If the semicolon were added at line 7, then answer B would be correct—the program would print true, the return from the equals() method overridden by the anonymous subclass of Object.

10. 

þ  

B. The code in the HorseTest class is perfectly legal. Line 9 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 10 casts the Horse object to a Horse reference variable, which allows line 11 to compile. If line 10 were removed, the HorseTest, code would not compile, because class Object does not have a name variable.

ý  

A, C, D, E, and F are incorrect based on the program logic described above.

11. 

þ  

E. This code is identical to the code in question 10, except the casting statement has been removed. If you use a reference variable of type object, you can access only those members defined in class Object.

ý  

A, B, C, and D are incorrect based on the program logic described above.

12. 

þ  

A. You can define an inner class as abstract, which meansyou can instantiate only concrete subclasses of the abstract inner class. The object referenced by.the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to create a Bar instance, we need an instance of the enclosing AbstractTest class to tie. to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.

ý  

B, C, D, E, and F are incorrect based on the program logic described above.




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net