You are on page 1of 1

Anonymous Classes (Declarations,access ctrl) Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
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
1 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
An anonymous class is
anonymous class can extend a named class including Object. An anonymous class
implicitly final. A static
b can not be extended; so it can not be abstract. An anonymous class can not be
2 reference variable can
d declared static; but a static reference variable can refer to an anonymous class. An
reference an anonymous
anonymous class declaration can not contain an explicit constructor declaration, but
class.
an anonymous class declaration can contain an instance initializer.
An anonymous class declaration can not contain an explicit declaration of a
3 c Compile-time error
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
c
4 3 5 long after the method runs to completion. An object that is instantiated within a
e
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.
5 c Compile-time error
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
6 c Compile-time error
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
7 b Prints: 3122
class are passed to a constructor of the superclass.

page:1

You might also like