Home / Java Programming / Declarations and Access Control :: Discussion

Discussion :: Declarations and Access Control

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

  2. A.

    int

    B.

    byte

    C.

    long

    D.

    double

    View Answer

    Workspace

    Answer : Option D

    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.

     


Be The First To Comment