| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice Test
Share
medium Solved by 4 students
Recursion to Iteration Conversion
< Prev
Next >

Which, if any, of the following changes to mystery2 is required so that the two methods work as intended?
Consider the following two methods, which are intended to return the same values when they are called with the same positive integer parameter n.

public static int mystery1(int n)
{
    if (n > 1)
    {
        return 5 + mystery1(n - 1);
    }
    else
    {
        return 1;
    }
}
public static int mystery2(int n)
{
    int total = 0;
    int x = 1;
    while (x < n)
    {
        total += 5;
        x++;
    }
    return total;
}

Which, if any, of the following changes to mystery2 is required so that the two methods work as intended?

A

The variable x should be initialized to 0.

B

The condition in the while loop header should be x < n - 1.

C

The variable total should be initialized to 1.

D

No change is required; the methods, as currently written, return the same values when they are called with the same positive integer parameter n.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1singhris00012 1m 29s 1
#2bommasam00012 1m 58s -28
#3richa.tuli12 5m 50s -260
APFIVE © 2020.
Email: [email protected]|Privacy Policy