0

What is the right answer?

public static int someCode(int a, int b, int c){ if((a<b) && (b<c)) { return a; } if((a>=b) && (b>=c)) { return b; } if((a==b) || (a==c) || (b==c)) { return c; } } Pick one answer below that describes the error in above code. java code 1 Is it possible to reach the end of the method without returning a value 2 The third if statement is not reachable 3The if statements must have else parts when they contain return statements 4Methods cannot have multiple return statements 5 The keyword word return cannot be used in the body of an if statement

5th Jan 2019, 10:39 AM
Skyler
Skyler - avatar
7 Antworten
+ 1
Niwo Ah, yes, that too. Same goes for the "largest value" check, where b has to be >= c.
5th Jan 2019, 1:59 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Based on method of elimination, 2, 3, 4, and 5 do not cause errors and hence do not contain the correct answers. With close inspection, you can see that the code only returns if a is the smallest value or a is the largest value or any of the values are equivalent If b or c is larger than a, the function would fail to return anything. It is also note worthy that Java would warn you if all your return statements in a function are written within if blocks, since it is possible for the function to end without returning anything.
5th Jan 2019, 1:28 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
a can be the smallest value and it wont return anything. if a is the smallest then b must be greater than c...
5th Jan 2019, 1:39 PM
Niwo
Niwo - avatar
0
1. is true. If a=3 b=5 c=4 you wouldn't get a return... You could remove this possibility if you change the second if to an else if and the third if to an else. The function ends anyway as soon as a return is executed, so it wouldn't make a different...
5th Jan 2019, 1:27 PM
Niwo
Niwo - avatar
0
Hatsy Rei Yes and if a is the largest, b must be smaller then c...
5th Jan 2019, 2:35 PM
Niwo
Niwo - avatar
0
Thanks
8th Jan 2019, 6:06 AM
Skyler
Skyler - avatar
0
so what is the final answer then?
27th Aug 2020, 8:09 AM
[No Name]