Witscale Test Center

Answers with explanations


Answers with explanations

1. ž E F

E and F are correct. The expression b + c is a binary expression, therefore the arithmetic promotion of operands takes place. As per the fourth rule of arithmetic promotion, if one of the operands is neither double, float nor long then both are promoted into int. The operands b is byte and c is char, they both are promoted to int. The value of expression b + c will be of type int. An int value can be stored in variable of type int, long, float or double. Since the given options have int and float, they are the correct answers.

B C and D are incorrect. If the type of summation is any of these, it will cause a compiler error as in that case we will be storing a wider int value into variable of narrower type. A and G are invalid. void and null can not be used as variable types. void can be used only as a return type in method declaration and  null is a value and not a data type.

 

2. ž C D

C and D are correct. Arithmetic promotions are relevant only for the non-boolean primitives. Object references are not involved in any type of arithmetic. D is correct; if you cast an object reference to a type and if the object referenced by that object reference can notbe treated as that type, then Java runtime system throws the ClassCastException.

E is incorrect; the casting of primitive is validated at compile time itself. Therefore, it does not throw runtime exception. A is incorrect as casting is not required when the type can be automatically converted. B is incorrect as casting is required in primitives when you want to change a narrower datatype to wider datatype.

 

3. ž C

The correct answer is C. Initially byte value is 6. The increment method that takes byte argument will be called. This method increment() takes a copy of its argument and increments it by 2. When this method returns, value of b in main is still 6. Hence 6 byte 6 is printed. Note that method does not modify a primitive argument as argument’s copy is sent to the method.

 

4. ž D

The correct answer is D. Initially byte value b is 6, it will remain 6 even after method call. When the increment method is called with a byte argument, Java will not find any such method. Instead of throwing any exception, Java continues its search for increment method with compatible wider type. Since there is a increment method that takes int parameter, it will be called and byte value 6 will be first converted to int and then passed as argument. Hence 6 int 6 is printed. Note that increment method that takes char will not be called, as byte is not automatically convertible to char.

 

5. ž E

The correct answer is E. This code will fail to compile at line 5. This compiler error occurs as the ternary expression returns a double value, which cannot be stored in int variable j. Like the binary expression, the type of ternary expression depends on its operands. Since first operand is the condition that has to be boolean. Hence the type depends on the type of second and third operand. In the ternary expression at line 5, 121.0 and i—are the two operands. The type of floating-point literal 121.0 is double. As per the rules of arithmetic promotion, the type of second operand i— is promoted from int to double. The ternary expression returns a double value for i— that is 11.0 as the condition (i%2 == 0) is false. But the double value 11.0 cannot be stored into int variable j as it is. Hence, compiler throws an error.

 

6. ž A B C

The options A, B and C represent the valid ways of calling the mow() method. This method accepts argument of interface-type Mowable. Any class that directly or indirectly implements Mowable is of interface-type Mowable. Therefore you can pass objects of any class that implements Mowable as an argument to the mow() method. The class TurfGrass does not implement Mowable, but its superclass does. Hence the class TurfGrass indirectly implements that interface. Therefore, we can pass its object to a method that expects argument of type Mowable.

D is incorrect as interface is never instantiated. Only classes can be instantiated with new keyword. E is invalid as it is sending a String object to mow() method. The String type and Mowable are incompatible as they belong to different class hierarchies. Therefore we cannot send string to mow() method.

 

7. ž D

The code will fail to compile at line 14 as we are assigning a wider type Mowable to narrower type Grass. The simple meaning of Grass implements interface Mowable is that “Every grass is mowable. But every mowable thing may not grass”.

Line 11 will compile as the reference of subclass type TurfGrass is assigned to the variable grass of class-type Grass. Since Grass is the superclass of TurfGrass, automatic type conversion takes place in grass = turfGrass. Similarly line 12 compiles as Object is superclass of Weed and automatic type conversion takes place Object o = weed; 

 

8. ž B

The code will compile successfully. The casting at line 10 is valid at compile time. The compiler cannot be sure of what type of object the variable grass will point at compile time. At runtime, however JVM knows that grass is pointing to Grass object and hence it cannot be treated as a TurfGrass object. (“turf grass is always a (kind-of) grass, but grass is not a turfgrass.) Therefore, the casting at line 10 causes a ClassClastException at runtime.

 

9. ž E

The code will compile successfully. The casting at line 8 is valid at compile time. The compiler cannot be sure of the type of object the variable o will point at compile time. At runtime however JVM knows that o is pointing to an array object of double[] type. Remember that primitive arrays cannot be converted or cast to other primitive arrays. Therefore casting of double[] referenced by o to float[] will cause an ClassClastException at runtime.

A is incorrect as long value wholesalePrice can be store in prices[i] which is of type double because the type double is wider than long. B is incorrect as array reference prices can be stored in variable of Object type. Remember that array is an object of array class and this array class has Object class as its superclass. Therefore the Object type is wider than double[] type. .Hence o can store reference prices . C is incorrect as object reference of Object type can be cast to any array

reference, as array is subclass of Object. D is incorrect as long value wholesalePrice can be stored in costs[i]. The elements of costs are of type float and float is wider type than long.

                                                                                                                                            

10. ž C

The code will compile successfully. The array reference prices is passed to method processIT(). This method copies this prices reference into method variable arr and modifies the float array. The arr and prices are referencing the same float array. Hence, when the processIt method is executed, the main method can access the modified array through prices.