Home » Java Programming » Declarations and Access Control

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; } }

Correct Answer: double

Explanation:

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.



← Previous Question Next Question→

More Questions from Declarations and Access Control

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion