Question: The following code is intended to return only even numbers greater than 0 . If an even number greater than 0 is passed to the

The following code is intended to return only even numbers greater than 0. If an even number greater than 0 is passed to the method, it should return that number. If an odd number or zero is passed, it should return the next highest even number.
1: public int returnEven(int number)
2: {
3: if (number %2==0)
4: {
5: return number;
6: }
7: else if (number ==0)
8: {
9: return number +2;
10: }
11: else
12: {
13: return number +1;
14: }
15: }
Java
Does the code work as intended?
Yes.
Incorrect Answer
No, the mod function on line 3 should read number %2==1 to find even numbers.
No, the else if on line 7 and the else on line 11 should just be if statements.
Correct Answer
No. Zero will get returned on line 5 and not make it to line 7.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!