Anonymous Classes

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • 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 as PDF for free.

More details

  • Words: 1,448
  • Pages: 6
Anonymous Classes Question 1 Which of the following is a true statement? a. b. c. d.

An anonymous class can extend only the Object class. An anonymous class can not implement an interface. An anonymous class declaration can not have an implements clause. An anonymous class declaration can name more than one interface in the implements clause. e. The class instance creation expression for an anonymous class must never include arguments. f. None of the above ANSWER 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() An anonymous class {}} creates an instance of an anonymous class that declaration can not extends Object and implements a method named 1 c have an doNothing. In other words, if a class name immediately implements follows the keyword new, then the anonymous class clause. 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.

Question 2 Which of the following are true statements? a. An anonymous class is implicitly abstract. b. An anonymous class is implicitly final. c. An anonymous class is implicitly static.

d. A static reference variable can reference an instance of an anonymous class. e. An anonymous class declaration must have at least one explicit constructor declaration. f. An anonymous class declaration can have more than one explicit constructor declaration. ANSWER 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 An anonymous class is creates an instance of a class; so it is not surprising implicitly final. A that an anonymous class can not be declared b static reference 2 static. Even so, a static reference variable can d variable can reference an refer to an anonymous class. A constructor shares the instance of an anonymous same name as the class in which it is declared, but an class. 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.

Question 3 abstract class A { private int x = 4, y = 2; public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} public abstract int math(); } class B { static A a1 = new A(2,1) { public A(int i1, int i2) {x(i1);y(i2);}; public int math() {return x()+y();} }; public static void main(String[] args) { System.out.print(a1.math()); }}

What is the result of attempting to compile and run the program? a. Prints: 8 b. Prints: 3122 c. Compile-time error

d. Run-time error e. None of the above ANSWER Compile-time 3 c error

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

Question 4 class A { private static int f1 = 1; private int f2 = 2; void m1(int p1, final int p2) { int l1 = 5; final int l2 = 6; Object x = new Object() { int a = f1; // 1 int b = f2; // 2 int c = p1; // 3 int d = p2; // 4 int e = l1; // 5 int f = l2; // 6 };}}

Compile-time errors are generated at which lines? a. b. c. d. e. f.

1 2 3 4 5 6 ANSWER 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 c 3 can continue to exist long after the method runs to completion. An object that 4 e 5 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.

Question 5 abstract class A {

private int x = 1, y = 1; public A(int x, int y) {this.x = x; this.y = y;} public abstract int math();

} class B { static A a1 = new A(2,1) {public int math() {return static A a2 = new A(2,1) {public int math() {return static A a3 = new A(2,1) {public int math() {return static A a4 = new A(2,1) {public int math() {return public static void main(String[] args) { System.out.print("" + a1.math() + a2.math() + a3.math() + a4.math()); }}

x x x x

+ * /

y;}}; y;}}; y;}}; y;}};

What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: 3122 Prints: 2011 Compile-time error Run-time error None of the above ANSWER 5 c

Compiletime error

The instance variables x and y in class A are private, so they are not accessible to the anonymous subclasses. If you want to access the private variables of a superclass, then you will need to add accessor methods to the superclass such as getX and getY.

Question 6 abstract class A { private int x = 4, y = 2; public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} public abstract int math(); } class B { static A a1 = new A() {public int math() {return static A a2 = new A() {public int math() {return static A a3 = new A() {public int math() {return static A a4 = new A() {public int math() {return public static void main(String[] args) { System.out.print("" + a1.math() + a2.math() + a3.math() + a4.math()); }}

x()+y();}} x()-y();}} x()*y();}} x()/y();}}

What is the result of attempting to compile and run the program?

a. b. c. d. e.

Prints: 18 Prints: 6282 Compile-time error Run-time error None of the above ANSWER Compile6 c 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.

Question 7 class A {String m1() {return "A.m1";}} interface B {String m2();} class C { static class D extends A implements B { public String m1() {return "D.m1";} public String m2() {return "D.m2";} } static A a1 = new A() implements B { public String m1() {return "m1";} public String m2() {return "m2";} }; public static void main(String[] args) { System.out.print(a1.m1() + "," + new C.D().m2()); }}

What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: m1,D.m2 Prints: A.m1,D.m2 Compile-time error Run-time error None of the above ANSWER An anonymous class can extend Object and implement an interface, or the anonymous class can extend a named class including Compile- Object. An anonymous class declaration can not have an 7 c time error 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.

Question 8 abstract class A { private int x = 4, y = 2; public A(int i1, int i2) {x=i1;y=i2;} public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} public abstract int math(); } class B { static A a1 = new A(2,1) {public int math() {return static A a2 = new A(2,1) {public int math() {return static A a3 = new A(2,1) {public int math() {return static A a4 = new A(2,1) {public int math() {return public static void main(String[] args) { System.out.print("" + a1.math() + a2.math() + a3.math() + a4.math()); }}

x()+y();}}; x()-y();}}; x()*y();}}; x()/y();}};

What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: 8 Prints: 3122 Compile-time error Run-time error None of the above ANSWER Prints: 8 b 3122

The arguments that appear in the class instance creation expression of an anonymous class are passed to a constructor of the superclass.

Related Documents

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

More Documents from ""