Witscale Test Center

Answers with explanations


Answers with explanations

1.       ž B, D and E

All three B, D and E are invalid declarations. B is invalid as a variable can never be abstract. D is invalid as an abstract method cannot be declared as private. When the method is private it cannot be overridden by the subclasses. But abstract method mandates that subclasses must override it. To avoid this contradiction, Java does not allow you to declare a method as abstract and private at the same time. E is invalid as there is no modifier called default.

A is valid declaration as variables can be declared as transient. C is also valid declaration. A native method can be static as well as final. None of the modifiers is contradicting to each other. Don’t get confused with char in the declaration. It is the return type of method linefeed().

 

2.       ž D

The code will compile and print 4 on console. birdCount is increased each time a Bird object is created on line 11,12 and 14. It will also increment on line 13 where the static variable is accessed via class name. They all are accessing the same copy of birdCount.

C is incorrect as constructor method can always access the static variable just like any other instance method. F is incorrect as static variables can be accessed without creating any instance.

 

3.       ž E

The code will compile and prints 15 on console. The two static blocks, constructor as well as the main() method will have access to static variable s. At the time of class load, the two static code blocks are executed making value of s as 15.  The main method prints value of s as 15. The constructor is not called anywhere so its code will not be executed.

A is incorrect; the static variables are automatically initialized when the class is loaded. F is also incorrect; the class can have more than one static blocks.

 

4.       ž A

A is correct; the final class cannot be subclassed, so it may never implement any abstract methods.

Option C is not correct. A final class can have native methods. Native methods suggest that their implementation lies in native library. They do not force subclasses to implement it. Therefore, final class can have native methods. Option B is also not true as abstract class can have final methods. Final methods cannot be overridden in subclasses, but declaring final method in abstract class does not interfere with the other enforcements of abstract class.

 

5.       ž C E

C and E both are true statements. C is correct as the final variables are constants and they must be initialized to that constant value. E is correct as the keyword transient is used to avoid serialization of variable.

A is incorrect; the static variable is not constant and its value can be changed. B is incorrect, as the transient modifier does not apply to methods.  D is incorrect, as class cannot be declared as native.

 

6.       ž B

B is correct. The static variable family is initialized to null. In the given code, its value is changing only in the constructors of Lion and HouseCat. So it will be null if no instance is created.

A is incorrect as protected method move() of Cat class is  accessible to Lion and Lion may override it. D is incorrect as family is protected so it is accessible in subclass constructor. E is incorrect as well; while it is true that all newly created Lion object will have “BigCat” as their family, the family is a static variable and can be changed by anyone with access to it. So we cannot say for sure that the value will be always be “BigCat”.

 

7.       ž A

The family variable will be assigned to “feline” when HouseCat object is created. Since HouseCat does not have a constructor, the compiler will create a default constructor (that takes no argument) . The default constructor always calls the zero argument constructor of the superclass. Hence constructor of  Cat is called implicitly. This constructor of Cat will assign the value “feline” to the family variable. The variable cat is then assigned to a Lion object. The constructor of Lion assigns family to “BigCat”. Both the objects, Lion and HouseCat, access the same copy of family.

D is incorrect. Tester class will have access to family variable of Cat class as it lies in same package as the Cat class.

 

8.       ž D F G

All D, F and G are valid constructors.

A is incorrect. Though it is a legal method declaration, it is not a constructor. Remember that constructor does not have a return type, not even void. B & C are invalid as constructors can never be final or abstract. E is incorrect. The constructor’s name must be exactly same as class name. So “Point” is acceptable as constructor name, “point” is not.