| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | An anonymous class declaration can not have an implements clause. | A class instance creation expression can create an instance of a named class or an anonymous class. For example, the class instance creation expression new Object() creates an instance of the class named Object. If a class body appears in the class instance creation expression, then an anonymous class is created. For example, the expression new Object() {void doNothing(){}} creates an instance of an anonymous class that extends Object and implements a method named doNothing. In other words, if a class name immediately follows the keyword new, then the anonymous class extends the named class. When a named class is being extended, then the class instance creation expression can contain an optional argument list. The arguments will be passed to the direct superclass constructor that has a matching parameter list. An anonymous class declaration can not have an implements clause or an extends clause. |
| 2 | b d | An anonymous class is implicitly final. A static reference variable can reference an instance of an anonymous class. | An anonymous class can extend Object and implement an interface, or the anonymous class can extend a named class including Object. An anonymous class can not be extended; so it can not be abstract. An anonymous class declaration always creates an instance of a class; so it is not surprising that an anonymous class can not be declared static. Even so, a static reference variable can refer to an anonymous class. A constructor shares the same name as the class in which it is declared, but an anonymous class has no name. For that reason, it is not surprising that an anonymous class declaration can not contain an explicit constructor declaration. Instead, an anonymous class can contain an instance initializer. |
| 3 | c | Compile-time error | An anonymous class declaration can not contain an explicit declaration of a constructor. |
| 4 | c e | 3 5 | Local method variables and method parameters are stored on the stack and go out of scope after the method is exited. Although a local reference variable is stored on the stack, the referenced object is stored on the heap; so the object can continue to exist long after the method runs to completion. An object that is instantiated within a method or block is not permitted to refer to a variable that is declared within the method or block unless the variable is declared final and the variable declaration precedes the creation of the object. |
| 5 | c | Compile-time error | When an anonymous class declaration is the last thing that appears in a statement, then a semicolon must follow the declaration. Anonymous class declarations provide an excellent opportunity for trick questions involving statements with missing semicolons. |
| 6 | c | Compile-time error | An anonymous class can extend Object and implement an interface, or the anonymous class can extend a named class including Object. An anonymous class declaration can not have an implements clause. In this case, the declaration of the anonymous class referenced by a1 generates a compile-time error as a result of the attempt to extend class A and implement interface B. |
| 7 | b | Prints: 3122 | The arguments that appear in the class instance creation expression of an anonymous class are passed to a constructor of the superclass. |