Friday, February 25, 2011

Self Test: Chapters 52 to 55

In this chapter, we are going to take a look at some questions on the Inner class topics we had seen in the past few chapters.

All the best!!!

Questions:

Question 1.

Which are true about a static nested class? (Choose all that apply.)
A. You must have a reference to an instance of the enclosing class in order to instantiate it
B. It does not have access to non-static members of the enclosing class
C. Its variables and methods must be static
D. If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner();
E. It must extend the enclosing class

Question 2.

Given:
class Parent {
Parent(String s) { }
Parent() { }
}
class Child extends Parent {
Child() { }
Child(String s) {super(s);}
void dooo() {
// insert code here
}
}

Which create an anonymous inner class from within class Child? (Choose all that apply.)
A. Parent f = new Parent(24) { };
B. Parent f = new Child() { };
C. Parent f = new Parent() {String s; };
D. Child f = new Parent(String s) { };
E. Parent f = new Parent.Child(String s) { };

Question 3.

Which are true about a method-local inner class? (Choose all that apply.)
A. It must be marked final
B. It can be marked abstract
C. It can be marked public
D. It can be marked static
E. It can access private members of the enclosing class

Question 4.

Given:
1. public class TestClass {
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("Rocky"));
9. }
10. }

What is the result?
A. An exception occurs at runtime
B. true
C. Rocky
D. Compilation fails because of an error on line 3
E. Compilation fails because of an error on line 4
F. Compilation fails because of an error on line 8
G. Compilation fails because of an error on a line other than 3, 4, or 8

Question 5.

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

What is the result?
A. An exception occurs at runtime at line 10
B. Ferrari
C. Compilation fails because of an error on line 3
D. Compilation fails because of an error on line 9
E. Compilation fails because of an error on line 10

Question 6.

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



What is the result?
A. 57 22
B. 45 38
C. 45 57
D. An exception occurs at runtime
E. Compilation fails

Question 7.

Given:
3. public class Test {
4. public static void main(String[] args) {
5. Outer c = new Outer();
6. // insert code here
7. s.go();
8. }
9. }
10. class Outer {
11. class Inner {
12. void go() { System.out.println("hiii"); }
13. }
14. }

Which, inserted independently at line 6, compile and produce the output “hiii”? (Choose all that apply.)
A. Inner s = c.new Inner();
B. c.Inner s = c.new Inner();
C. c.Inner s = Outer.new Inner();
D. Outer.Inner s = c.new Inner();
E. Outer.Inner s = Outer.new Inner();

Question 8.

Given:
5. class A { void m() { System.out.println("outer"); } }
6.
7. public class TestInnerClass {
8. public static void main(String[] args) {
9. new TestInnerClass().go();
10. }
11. void go() {
12. new A().m();
13. class A { void m() { System.out.println("inner"); } }
14. }
15. class A { void m() { System.out.println("middle"); } }
16. }

What is the result?
A. inner
B. outer
C. middle
D. Compilation fails
E. An exception is thrown at runtime

Question 9.

Given:
3. public class Car {
4. class Engine {
5. // insert code here
6. }
7. public static void main(String[] args) {
8. new Car().go();
9. }
10. void go() {
11. new Engine();
12. }
13. void drive() { System.out.println("hi"); }
14. }

Which, inserted independently at line 5, produce the output “hi”? (Choose all that apply.)
A. { Car.drive(); }
B. { this.drive(); }
C. { Car.this.drive(); }
D. { this.Car.this.drive(); }
E. Engine() { Car.drive(); }
F. Engine() { this.drive(); }
G. Engine() { Car.this.drive(); }

Question 10.

Given:
3. public class City {
4. class Chennai {
5. void doSomething() throws Exception { System.out.print("x "); }
6. }
7. class MarinaBeach extends Chennai {
8. void doSomething() throws Exception { }
9. }
10. public static void main(String[] args) throws Exception {
11. new City().go();
12. }
13. void go() throws Exception { new MarinaBeach().doSomething(); }
14. }

What is the result?
A. x
B. x x
C. No output is produced
D. Compilation fails due to multiple errors
E. Compilation fails due only to an error on line 4
F. Compilation fails due only to an error on line 7
G. Compilation fails due only to an error on line 10
H. Compilation fails due only to an error on line 13


Self Test Answers:

Answer 1.

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 because...it just is. There’s no rule that says an inner or nested class has to extend anything.

Answer 2.

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 Child. Since Child is a subclass of Parent, it all works. C uses correct syntax for creating an instance of Parent.

A is incorrect because it passes an int to the Parent constructor, and there is no matching constructor in the Parent 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.

Answer 3.

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.

Answer 4.

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.

Answer 5.

E. If you use a reference variable of type Object, you can access only those members defined in class Object.

Answer 6.

A. You can define an inner class as abstract, which means you 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 TestAbstractEx, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Child, and the anonymous Child subclass also overrides the getNum() method (to return 57). Remember that to create a Child instance, we need an instance of the enclosing TestAbstractEx class to tie to the new Child inner class instance. TestAbstractEx 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 Child subclass instance.

Answer 7.

D is correct. It is the only code that uses the correct inner class instantiation syntax.

A, B, C, and E are incorrect based on the above. (Objective 1.1)

Answer 8.

C is correct. The "inner" version of class A isn’t used because its declaration comes after the instance of class A is created in the go() method.

Answer 9.

C and G are correct. C is the correct syntax to access an inner class’s outer instance method from an initialization block, and G is the correct syntax to access it from a constructor.

Answer 10.

C is correct. The inner classes are valid, and all the methods (including main()), correctly throw an Exception, given that doSomething() throws an Exception. The doSomething() in class MarinaBeach overrides class Chennai’s doSomething() and produces no output.

Previous Chapter: Quick Review - Inner classes

Next Chapter: Chapter 56 - Threads & Multithreading

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers