Witscale Test Center

Answers with explanations


Answers with explanations

1.    þ B

Only B represents the valid signature. It is mandatory for main to have string array as argument type. The name of the argument can be anything (of course it must be a valid java identifier).

A is invalid as its return type is int. C is invalid as you must have the modifiers before the return type in any method declaration. C, D, E and F are also invalid as the argument to main must be a string array. G is invalid as the main method must be a static method.

 

2.    þ D

Only D is true. The package statement cannot come after the import statements.

B is not true; there is no such restriction on the order of top-level classes. E is invalid as it is not mandatory for a class to have an main method for successful compilation.

 

3.    þ F

This code will compile but fails to run. If you run it with the given 3 arguments, the three arguments Frisky, Mickey and Longtail will go in args[0],args[1] and args[2] respectively. At line 8 while trying to access the args[3], the ArrayIndexOutOfBoundsException occurs as there is nothing on index 3.

 

4.    þ A, B, D

A, B and D are false statements. A is incorrect as default value for variable of String type is null and not an empty string. B is incorrect as default value for boolean variables is false. D is incorrect as default value for float variables is 0.0f.

D is false and E is true because, 0.0 is a floating point literal and it is of type double. Therefore, we can safely say that 0.0 is default value for double as 0.0d and 0.0 are same. However, we cannot say that 0.0 is default value for float as 0.0f and 0.0 are not same. F is correct as variable of any object reference type have null as default value.

 

5.    þ  E

The code will fail to compile at line 11 as the local variable applicationDate is used (in the condition in the if statement) before it is initialized. C is wrong as the main method belongs to the MortgageApplication class and all the methods of a class have access to its own the private methods.

(Tip: In exam, you will often meet such camouflage questions where the question appears to be checking something else. Here the options do not give a clue whether the question is testing the knowledge of access modifiers or automatic initialization. You need to know both the concepts well to answer correctly.)

 

6.    þ  F

Only option F represents the correct implementation. Runnable interface has only one method. Since MyThread is declared as abstract, it is ok even if it does not provide implementation of that run method.

A, E are invalid as you cannot extend from an interface. B, C, D, E are invalid as they do not properly implement the only method (public void run()) of Runnable. F too does not implement it. However, it is declared as abstract, therefore it is deferring its implementation to its subclasses.

 

7.    þ  E

E is correct. The method compiles successfully. All variables are initialized before their first use.

A is incorrect as though result is not initialized at line 5, the compiler does not flag an error until it is used without initialization. C is incorrect as variable result is initialized at line 6. D is incorrect as variable a and b are method parameters. They are initialized at the start of method execution to the arguments passed. Therefore they are not un-initialized.

Tip: In the exam, you may face a code, which is numbered. When the line numbers start from 1, you can assume that the code is standalone. However, if it starts with arbitrary line number, it is usually a code fragment. You can then make certain general assumptions about it. For instance, in case of listing 8.5, you can assume that the method is declared in a proper class definition.

 

8.    þ  E

E is correct. The method compiles successfully. All variables are initialized before their first use.

A is incorrect as though result is not initialized at line 5, the compiler does not flag an error until it is used without initialization. B is incorrect as variable a and b are method parameters. They are initialized at the start of method execution to the arguments passed. Therefore, they are never un-initialized within the method. C and D are incorrect. Though result is being initialized in condition, it is initialized in both the if part and the else part. Since one of them is ought to executing at runtime, the compiler knows for sure that it will be initialized no matter what the value of b is.