Answer:
e. II and III
Step-by-step explanation:
Given
The above code segment
Required
Which can complete the updateAge() method
From the program we understand that the updateAge() will update the year and month based on the extraMonths passed to it.
For instance
and
updateAge(5) will update years to 7 and month 1
Having established that, next we analyze options I, II and III
Code I
1. This divides extraMonths by 12 and saves the remainder in yrs.
For instance: 15 months = 1 year and 3 months. So:
2. This divides extraMonths by 12 and saves the whole part in mos
For instance: 15 months = 1 year and 3 months. So:
3. This updates the value of years by the result of 1 (i.e. the remaining months)
4. This updates the value of years by the result of 2 (i.e. the remaining years)
Conclusion:, (I) is incorrect because years and months were updated with the wrong values
Code II
1. This converts years to months, then add the result of the conversion to extraMonths + month
For instance:
2. This calculates the number of years in totalMonths
i.e
3. This divides totalMonths by 12 and saves the remainder in months
i.e.
Rough Calculation
Conclusion: Code II is correct
Code III
1. This calculates the total months
For instance:
2. This calculates the number of years in totalMonths, then add the result ot years
i.e.
3. This divides totalMonths by 12 and saves the remainder in months
i.e.
Rough Calculation
Conclusion: Code III is correct