Declarations and Access Control

Q1: What is the widest valid returnType for methodA in line 3?
public class ReturnIt 
{ 
    returnType methodA(byte x, double y) /* Line 3 */
    { 
        return (long)x / y * 2; 
    } 
}

A int

B byte

C long

D double

ANS:A - int

However A, B and C are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion to double. Java's widening conversions are: - From a byte to a short, an int, a long, a float, or a double. - From a short, an int, a long, a float, or a double. - From a char to an int, a long, a float, or a double. - From an int to a long, a float, or a double. - From a long to a float, or a double. - From a float to a double.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.