Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
medium Solved by 3 students

Recursion to Iteration Conversion

< Prev
Next >

Consider the following two methods, which are intended to return the same value for any positive integer 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 of the following changes to mystery2, if any, is required for the methods to work as intended?

A

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

B

The variable total should be initialized to 1.

C

The variable x should be initialized to 0.

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
Report Question
Mark for review

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1singhris00012 1m 29s 1
#2bommasam00012 1m 58s -28
#3richa.tuli12 5m 50s -260
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|FAQ