Witscale Test Center

 Answers with explanations


 Answers with explanations

1.       þ A, F

A and F both are true statements about the static nested classes.

B is incorrect as there is no such restriction on members of static nested class. C is incorrect as static nested class like any other static feature does not need an instance of the enclosing class for access. D is incorrect, static nested class can only be declared within a class-body, they cannot be declared within a method or block. E does not apply to static nested classes.

 

2.       þ  C, E

C and E are true statements about the member inner classes.

A is incorrect as member inner class like any other instance variable or method, has access to static features of the class. B is incorrect as there is no such restriction on members of member inner class. D is incorrect, member inner class can only be declared within a class-body, they cannot be declared within a method or block. F is also incorrect. The keyword this within the inner class refers to the inner class’s instance. Therefore when you say this.someVariable inside inner class, Java will assume that you are trying to access someVariable of inner class. If you want to access someVariable of outer class say OuterOne by referring to it either as someVariable or  as OuterOne.this.someVariable.

 

3.       þ B

Only B constructs the anonymous class instance properly.

The anonymous class implementing Paddleable interface must implement all its methods. A is incorrect as it declares the anonymous class properly but fails to implement paddle(). C, D, E and F are all incorrectly declaring the anonymous class. C is incorrect as it writes the method declaration inside the round parentheses. D is incorrect as the declaration of paddle method is not correct, the () is missing in the declaration. E is incorrect as the round parentheses after Paddleable are missing. F is incorrect as it writes a method declaration inside the round parentheses

 

4.        þ A

The code compiles and prints 51 on console. The anonymous class is extending an inner class AbstractScoreBoard.LiveScore and overriding its getScore() method.

D is incorrect, as line 4 does not instantiate an abstract class but rather extends it to an anonymous class and at the same time creates an instance of this anonymous class. It does all this in new AbstractScoreBoard() {};

 

 

5.       þ A, B, C, D and F

The class RoleDeveloper is declared within a method and it is a method-local class. The method-local inner classes have access to all member variables of the enclosing class and the final variables of the method.

E is incorrect; RoleDeveloper cannot access the format variable, as it is not declared final.

 

 

6.       þ B, D and E

B represents correct way of instantiating a member inner class. It is created by creating the outer class’s instance first. Both D & E are correct ways of creating instance of a static nested class. Though it is not necessary to have an instance of Outer class to create an instance of static nested class (as shown in D), it does not harm either.

A is incorrect as anonymous class is declared and instantiated at the same time, hence it cannot be instantiated the way shown in A. Since it implements ActionListener, it must implement it inside curly brackets. C is incorrect; the non-static member class Inner must have an instance of Outer class to create an instance.