Question

Question 3 Item 3 Consider the following method, which is intended to return the sum of...

Question 3

Item 3

Consider the following method, which is intended to return the sum of all the even digits in its parameter num.   For example, sumEvens(15555234) should return 6,   the sum of 2 and 4.

/**  Precondition:  num >= 0 */

public static int sumEvens(int num)

{

if (num < 10 && num % 2 == 0)

{

return num;

}

else if (num < 10)

{

return 0;

}

else if (num >= 10 && num % 2 == 0)

{

/*  missing statement */

}

else

{

return sumEvens(num / 10);

}

}

Which of the following can be used as a replacement for /* missing statement */ so that the sumEvens method works as intended?

A. return sumEvens(num % 10);

B. return sumEvens(num / 10);

C. return num % 10 + sumEvens(num % 10);

D. return num % 10 + sumEvens(num / 10);

E. return num / 10 + sumEvens(num % 10);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Above is a recursive function, and we need to find the recursive call in side, the if condition which checks for the number is greater than 10 and it is even,

ans : D. return num % 10 + sumEvens(num / 10);

let's check all the options with taking an example,

let's say my function input is 432

which will pass the if condition as it is both greater than 10 and even number(remainder 0 when divide with 2)

in this case according to the method definition we have to sum the even numbers, which means we have to add 2 from 432 to the total and send remaining digits 43 for further process

let's analyse the options that we have

num % 10 ==> 432 % 10 = 2
num / 10 ==> 432 / 10 = 43

A) return sumEvens( num % 10)

in the above statement we are not adding 2 to total, we are passing 2 for further process which is not right

B) return sumEvens( num / 10)

in the above statement we are passing 43 for further process, which is ok. but we are not adding 2 to total, which is wrong.

C) return num % 10 + sumEvens(num % 10)

in the above statement we are adding 2 to total, which is ok. but we are passing 2 only for further process which is wrong.

D) return num % 10 + sumEvens(num / 10)

in the above statement we are adding 2 to total, and passing 43 for further process which is right option

E) return num/10 + sumEvens(num % 10)

in the above statement we are adding 43 and passing 2 for further process which is wrong .

Add a comment
Know the answer?
Add Answer to:
Question 3 Item 3 Consider the following method, which is intended to return the sum of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT