Anonymous Classes - Answers

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Anonymous Classes - Answers as PDF for free.

More details

  • Words: 515
  • Pages: 1
Anonymous Classes (Declarations,access ctrl)

Answers

Answers: Certified Java Programmer Mock Exam No.

1

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 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 is implicitly final. A static b 2 reference variable can d reference 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 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.

3

c Compile-time error

An anonymous class declaration can not contain an explicit declaration of a constructor.

4

c 3 5 e

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

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!

c Compile-time error

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.

6

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.

page:1

Related Documents

Anonymous Classes
June 2020 19
Classes
April 2020 31
Classes
May 2020 17
Classes
July 2020 23