Witscale Test Center

Answers with explanations


Answers with explanations

 

                    1.                        þ C and F

Only the options C and F represent Java keyword. class and implements both are keywords.

Java keywords are always in lower case, therefore A and E should be eliminated as correct options. B and D are in lower case but they are not Java keywords. 

 

                    2.                        þ A, C D and E

A is valid; the string literals are string objects and they can be assigned to string variable. C is valid character literal. You can give hexadecimal value in character literal. 4ace is a valid hexadecimal number. D is valid because even if 1 is not a big number, it is appended by L, therefore it is a long literal and syntactically correct. E is correct, True is not a literal and it is not a reserved word either. It is a valid identifier. Therefore you can use it as a variable name. (However usually it is recommended to name variables in small case.)

B is invalid as 60.80 is a floating point literal and is of type double. Therefore, you cannot assign it to int variable like that.

 

                    3.                        þ C

The C represents invalid declaration; switch is a keyword. Hence you cannot use switch as a method name.

B is valid method declaration as the word “Class” is a valid identifier. A, D and E are also valid declarations; their method names as they follow the rules for a valid Java identifier. _$_ is a valid identifiers as it starts with underscore. assert_this is also valid identifier, it has two embedded keywords, but it is not a keyword.

 

                    4.                        þ B

Only B represents the correct range for byte values.

Remember, 0 is considered as positive number, therefore the range for signed numbers always have 1 less number of positives than the negative numbers. Therefore, C is invalid.  E is incorrect as it represents -129 to 128 as byte range. F is incorrect as byte is a signed number. It represents positive as well as negative values. G is also invalid; the range of primitive values does not change with platform.

 

                    5.                        þ B

B represents correct answer. The length is a variable of array object and not a method. It gives the array size.

C is incorrect as length gives you the real size of array. You need not subtract 1 to get the actual size. D and E are incorrect. There is no variable or method named size in array class.

 

                    6.                        þ A B D

A, B and D are valid declaration for two-dimensional array matrix. A and B are valid, it is ok  to declare a two dimensional array without the second dimension. D is correct as specifying it does not hurt either.

C is incorrect as it is assigning a variable matrix of integer array (int[]) type to two dimensional array of type int[][]. E is incorrect as the two dimensional array cannot be constructed without the first dimension in int[][5].

 

                    7.                        þ A

This code will fail to compile. numbers is a method variable which is only declared, but not initialized. The compiler checks whether the method variables are initialized before their first use. And flags an error if they are used before initialization.

B is incorrect; the compiler will catch this error at compile-time itself. The code is accessing element of array which is not created yet. F is incorrect; since the array is not created, there is no question of its elements being initialized.

 

                    8.                        þ D

D is the correct answer. The for loops with index i and j , are executed total a.length times, that is 3 times. Therefore in all, the statement System.out.println() is executed nine times.

A is incorrect. a[1] is initialized when the array a is created and contains null.(Tip: While solving this type of questions, make use of rough paper to draw a memory model as you go through the code.)