Anonymous Classes (Declarations,access ctrl

)
Answers: Certified Java Programmer Mock Exam No. Answer Remark

Answers

1

A class instance creation expression creates an instance of a class or an instance of an object that implements an interface. An unqualified class instance creation expression begins with the keyword new followed by the name of a class or interface type. An optional argument list can appear in parentheses. An anonymous class declaration is derived from a class instance creation expression that contains a class An anonymous class body. If a class name immediately follows the keyword new, then the anonymous c declaration can not have an class extends the named class. If an interface name immediately follows the keyword implements clause. new, then the anonymous class extends Object and implements the named interface. An anonymous class declaration can not have an implements clause or an extends clause. If the anonymous class extends a superclass, then the class instance creation expression can include an optional argument list. The arguments will be passed to a constructor of the superclass. If the anonymous class implements an interface, then the class instance creation expression can not include any arguments. 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 can not be declared static; but a static reference variable can refer to an anonymous class. An anonymous class declaration can not contain an explicit constructor declaration, but an anonymous class declaration can contain an instance initializer. An anonymous class declaration can not contain an explicit declaration of a constructor. 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. A semicolon is required at the end of each declaration of an anonymous class. Anytime an anonymous class appears on the exam, always check for the semicolon! An anonymous class can extend a superclass or it can implement an interface; however, an anonymous class declaration can not have an implements clause. In this case, the anonymous class referenced by a1 attempts to extend class A, but a compile-time error is generated due to the addition of the implements clause. The arguments that appear in the class instance creation expression of an anonymous class are passed to a constructor of the superclass.

An anonymous class is implicitly final. A static b 2 reference variable can d reference an anonymous class. 3 c Compile-time error

4

c 3 5 e

5

c Compile-time error

6

c Compile-time error

7 b Prints: 3122

page:1