Working with Methods Flashcards

1
Q

What should be the return type of the following method ?

public RETURNTYPE methodX(byte by) {
double d = 10.0;
return (long) by / d * 3;
}

-int , long , double, float, byte

A

double

Note that the cast (long) applies to ‘by’ not to the whole expression. ( (long) by ) / d * 3;

Now, division operation on long gives you a double.
So the return type should be double

public RETURNTYPE methodX(byte by) {
double d = 10.0;
return (long) by / d * 3;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly